1

input:

random adsf 845 asdsf.$Ecdda-string0.rand me39 84abd    
dee rand a[s% 845 a1sEdsf.$cdNda-string1.rand me39 84abd

output - keep everything between da- and .

string0    
string1

what I have tried so far:

sed -e 's/.*da-\(.*\)./\1/' file 
grep -o -P '(?<=da-).*(?=.)' file
grep -o -P '(?<=da-).*(?=\.)' file
Inian
  • 80,270
  • 14
  • 142
  • 161
achille
  • 173
  • 1
  • 3
  • 12

3 Answers3

0

Your sed was close. Needed to be \..* at the end of your matching

$ cat test.txt
random adsf 845 asdsf.$Ecdda-string0.rand me39 84abd
dee rand a[s% 845 a1sEdsf.$cdNda-string1.rand me39 84abd

$ sed 's#.*da-\([^.]*\)\..*#\1#g' test.txt
string0
string1
zzevannn
  • 3,414
  • 2
  • 12
  • 20
0

with grep look behind..

$ grep -oP '(?<=da-)[^.]+' file

string0
string1
karakfa
  • 66,216
  • 7
  • 41
  • 56
0
$ awk -F'[-.]' '{print $3}' file

string0
string1

Useing provided FS print third field.

Claes Wikner
  • 1,457
  • 1
  • 9
  • 8