0

I am trying to get string between device_uuid: and ,

d

device_uuid:xxx,
ptr
device_uuid:2,

command:

 sed -e 's/device_uuid:\(.*\),/\1/g' d

Output :

xxx
ptr
2

Expected output:

xxx
          == > blank as there is no pattern
2
user2711819
  • 960
  • 3
  • 16
  • 29

1 Answers1

2

Need some more advanced sed commands here:

sed 's/device_uuid:\([^,]*\),/\1/; tEnd; s/.*//; :End' <<DATA
device_uuid:xxx,
ptr
device_uuid:2,
DATA
xxx

2

The t command jumps to a label if the previous s command made a substitution, and the : command defined the label.

https://www.gnu.org/software/sed/manual/sed.html#Programming-Commands

May be easier to read with newlines instead of semicolons

sed '
    s/device_uuid:\([^,]*\),/\1/
    tEnd
    s/.*//
    :End
'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352