Issue
Trying to get the "Log" line printed only if "ref1" is present in the text following the "Log" line
sample text :
Log 0102
............
.....ref1.......
......ref1....
Log 0103
............
.....ref1.......
....
Log 0104
............
.....ref2.......
......
expected result :
Log 0102
Log 0103
result i am getting:
Log 0102
Log 0102
Log 0103
i tried this awk command but not getting results:
awk '/Log*/ line =$0}/ref1/{print line}'
Solution
With your shown samples please try following awk
code.
awk '
/^Log/{
if(found){ print value }
value=$0
found=""
next
}
/ref1/{
found=1
}
END{
if(found){
print value
}
}
' Input_file
OR a one-liner form of above code.
awk awk '/^Log/{if(found){print value};value=$0;found="";next} /ref1/{found=1} END{if(found){print value}}' Input_file
Answered By - RavinderSingh13 Answer Checked By - Pedro (WPSolving Volunteer)