I have an XML file in unix directory. I would like to search for some character if that is present, then grep the text located 3 lines before this matched line,.
Here is My File: (abc.xml)
<task Col="5" Object="BCD">
<checkpoint TcpOn="0"/>
<after ActFlg="0"/>
</task>
<task Col="6" Object="ABCD">
<checkpoint TcpOn="0"/>
<after ActFlg="1"/>
</task>
<task Col="7" Object="ABCDE">
<checkpoint TcpOn="0"/>
<after ActFlg="1"/>
</task>
Unix Code:
grep -i 'actflg="1"' abc.xml
Current Answer: This is returning the line where it is located.
<after ActFlg="1"/>
<after ActFlg="1"/>
What i Want is : (i want to do a further grep to display output as follows if actflg="1" is found...)
<task Col="6" Object="ABCD">
<task Col="7" Object="ABCDE">
You can use
grep -B XX
, which print XX lines before matching lines. Then you usehead -1
to just print the first one:In case there are multiple matches, you can do:
Explanation
'/ActFlg="1"/ {print preprev}
matches lines containingActFlg="1"
and does printpreprev
, a stored line.{preprev=prev; prev=$0}
this keeps storing the two previous lines. The 2 before is stored inpreprev
and the previous inprev
. So whenever we are in a new line, this gets updated.