Extract text between 2 lines only until the first occurrence of the trailing line

I am trying to extract the line between device_uuid: and immediate,

d

device_uuid:xxx,yyy,ttt
device_uuid:2,ppp,hhh

      

code:

$ sed -e 's/device_uuid:\(.*\),/\1/' d
xxx,yyyttt
2,ppphhh

      

O / p expected:

xxx
2

      

Edit 1:

Don't use grep -oP as I'm on AIX

awk crash:

d

device_uuid:xxx,yyy,ttt
ptr,ttt
device_uuid:2,ppp,hhh
total_uuid:5,jkl,mno
device_uuid:9

$ awk -F 'total_uuid:|,' '{print $2}' d
yyy
ttt
ppp
5

      

o / p is expected in the following case:

blank or device_uuid:xxx,yyy,ttt
blank or ptr,ttt
blank or device_uuid:2,ppp,hhh
5
blank or device_uuid:9

      

device_uuid: does not have to be the first column, for example wise, all can be random, but I need to select this variable corresponding to the fraction to the nearest delimiter,

- Cut also fails as it can only accept one char delimiter.

- $ perl -l -ne '/device_uuid:([\w\-]*?)\,/g and print($1)' d

above perl also fails, because if device_uuid is missing on a line then that line is removed at o / p, but it should show up as empty

Thank.

+3


source to share


2 answers


Usage sed

:

sed 's/^.*total_uuid:\([^,]*\).*$/\1/' file
device_uuid:xxx,yyy,ttt
ptr,ttt
device_uuid:2,ppp,hhh
5
device_uuid:9

      



Usage grep -oP

:

grep -oP 'device_uuid:\K[^,]+' file
xxx
2

      

+1


source


It's not pretty, but a couple cut

should get the job done:



$ echo "device_uuid:xxx,yyy,ttt" | cut -d":" -f2- | cut -d"," -f1
xxx

      

0


source







All Articles