Print all the lines between two patterns in shell -
i have file log of script running in daily cronjob. log file looks like-
aug 19 line1 line2 line3 line4 line5 line6 line7 line8 line9 aug 19 aug 20 line1 line2 line3 line4 line5 line6 line7 line8 line9 aug 20 aug 21 line1 line2 line3 line4 line5 line6 line7 line8 line9 aug 21
the log written script starting date , ending date , in between logs written.
now when try logs single day using command below -
sed -n '/aug 19/,/aug 19/p' filename
it displays output -
aug 19 line1 line2 line3 line4 line5 line6 line7 line8 line9 aug 19
but if try logs of multiple dates, logs of last day missing.
example- if run command
sed -n '/aug 19/,/aug 20/p' filename
the output looks -
aug 19 line1 line2 line3 line4 line5 line6 line7 line8 line9 aug 19 aug 20
i have gone through site , found valuable inputs similar problem none of solutions work me. links link 1
the commands have tried -
awk '/aug 15/{a=1}/aug 21/{print;a=0}a' awk '/aug 15/,/aug 21/' sed -n '/aug 15/,/aug 21/p grep -pzo "(?s)(aug 15(.*?)(aug 21|\z))"
but none of commands gives logs of last date, commands prints till 1st timestamp have shown above.
i think can use awk
command followed print lines between aug 19
& aug 20
,
awk '/aug 19/||/aug 20/{a++}a; a==4{a=0}' file
brief explanation,
/aug 19/||/aug 20/
: find record matchedaug 19
oraug 20
- if criteria met, set flag
a++
- if flag
a
in front of semicolon greater 0, print record. - final criteria, if
a==4
, reseta=0
, mind worked case in example, ifaug 19
oraug 20
more 4, modify number 4 in answer meet new request.
if want assign searched patterns variables, modify command followed,
$ b="aug 19" $ c="aug 20" $ awk -v b="$b" -v c="$c" '$0 ~ c||$0 ~ b{a++}a; a==4{a=0}' file
wiki
Comments
Post a Comment