linux - Extract string following another string in bash? -
linux - Extract string following another string in bash? -
i have next string:
[1360308597] service notification: qreda;demo-jms2;outconnectorresponse;notify-service-by-email;critical consumercount=0[1360308817] service notification: qreda;demo-jms2;disk space;critical;notify-service-by-email;disk critical - free space: /data 3018 mb (10% inode=92%):
in above string want extract string next demo-jms2
. here demo-jms2
occurs twice. want words next demo-jms2
reply should outconnectorresponse
, disk space
.
what want positive look-behind:
$ grep -po '(?<=demo-jms2;)[^;]+' file outconnectorresponse disk space
options:
-p, --perl-regexp pattern perl regular expression
-o, --only-matching show part of line matching pattern
explanation:
(?<=demo-jms2;) # positive lookbehind: match follow after literal demo-jms2; [^;]+ # match 1 or more non ; characters
edit:
to filter duplicates results pipe through sort -u
:
$ grep -po '(?<=demo-jms2;)[^;]+' file outconnectorresponse disk space outconnectorresponse disk space $ grep -po '(?<=demo-jms2;)[^;]+' file | sort -u disk space outconnectorresponse
linux bash grep design-patterns
Comments
Post a Comment