bash - regex backward find all text between a string and first occurrence of another string -
i need find recent occurrence of 'get' ( zzzz) before 'error:' , capture of text in between.
get xxxxx yyyyy zzzzz text more text error: error
can done?
edit
thanks, awk solution works, can further improved getting last occurrence of 'error:' ?
get xxxxx yyyyy zzzzz text more text error: first error xxxxx yyyyy zzzzz text more text error: last error
try following awk
solution:
awk ' /^get/ { delete lines; c=0; inblock=1 } /^error:/ { for(i=1; i<=c; ++i) print lines[i]; print; exit } inblock { lines[++c] = $0 } ' file
this assumes 1 block must printed, , error:
line should printed. (update: see below solution prints last block).
/^get/ { delete lines; c=0; inblock=1 }
starts building array of lines in variablelines
whenever stringget
encountered @ start of line./^error:/ { for(i=1; i<=c; ++i) print lines[i]; print; exit }
matches stringerror:
@ start of line , prints out lines built far, followed current line, , exits.inblock { lines[++c] = $0 }
adds every line starting recentget
line array.
update, per op's request:
to report (only) last block ends error:
, use following:
awk ' /^get/ { delete lines; c=0; inblock=1 } inblock { lines[++c] = $0 } /^error:/ { inblock=0; } end { for(i=1; i<=c; ++i) print lines[i] } ' file
this differs first solution in later blocks replace earlier ones, last block "wins", printed after input has been processed, in end
block of awk script.
Comments
Post a Comment