user2711819
user2711819

Reputation: 960

extract text between 2 strings only till first occurence of end string

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

d

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

code:

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

expected o/p:

xxx
2

Edit 1:

cant use grep -oP as i am on AIX

using awk fails :

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

expected o/p in above 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: need not be the first column , like wise all can be random but i need to pick that variable corresponding vale until the immediate 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 the device_uuid is not present in a line then that line is deleted in the o/p , but it should be displayed as blank

Thanks.

Upvotes: 3

Views: 790

Answers (2)

anubhava
anubhava

Reputation: 785721

Using sed:

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

Using grep -oP:

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

Upvotes: 1

Mureinik
Mureinik

Reputation: 311978

It ain't pretty, but a couple of cuts should do the job:

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

Upvotes: 0

Related Questions