regex - Sed to extract text between two strings -
please me in using sed. have file below.
start=a xxxxx xxxxx end start=a xxxxx xxxxx end start=a xxxxx xxxxx end start=b xxxxx xxxxx end start=a xxxxx xxxxx end start=c xxxxx xxxxx end start=a xxxxx xxxxx end start=d xxxxx xxxxx end
i want text between start=a, end. used below query.
sed '/^start=a/, / ^end/!d' input_file
the problem here , getting
start=a xxxxx xxxxx end start=d xxxxx xxxxx end
instead of
start=a xxxxx xxxxx end
sed finds greedily.
please me in resolvng this.
thanks in advance.
can use awk achieving above?
sed -n '/^start=a$/,/^end$/p' data
the -n
option means don't print default; script says 'do print between line containing start=a
, next end
.
you can awk
:
a pattern may consist of 2 patterns separated comma; in case, action performed lines occurrence of first pattern though occurrence of second.
(from man awk
on mac os x).
awk '/^start=a$/,/^end$/ { print }' data
given modified form of data file in question:
start=a xxx01 xxx02 end start=a xxx03 xxx04 end start=a xxx05 xxx06 end start=b xxx07 xxx08 end start=a xxx09 xxx10 end start=c xxx11 xxx12 end start=a xxx13 xxx14 end start=d xxx15 xxx16 end
the output using gnu sed
or mac os x (bsd) sed
, , using gnu awk
or bsd awk
, same:
start=a xxx01 xxx02 end start=a xxx03 xxx04 end start=a xxx05 xxx06 end start=a xxx09 xxx10 end start=a xxx13 xxx14 end
note how modified data file easier see various blocks of data printed came in file.
if have different output requirement (such 'only first block between start=a , end', or 'only last ...'), need articulate more in question.
Comments
Post a Comment