0

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">
4
  • 1
    You seem to be looking for a XML parser. See xmllint. Commented Feb 7, 2014 at 13:56
  • @devnull : No, it can any file.. this scenario is for xml file.. I have other files as well.. Commented Feb 7, 2014 at 13:56
  • possible duplicate of grep show 5 lines above grepped line Commented Feb 7, 2014 at 13:58
  • @devnull : This is not duplicate.. This is not working, if the same actflg="1" repeats multiple time Commented Feb 7, 2014 at 14:02

1 Answer 1

3

You can use grep -B XX, which print XX lines before matching lines. Then you use head -1 to just print the first one:

$ grep -B2 -i 'actflg="1"' file
<task Col="6" Object="ABCD">
<checkpoint TcpOn="0"/>
<after ActFlg="1"/>

$ grep -B2 -i 'actflg="1"' file | head -1
<task Col="6" Object="ABCD">

In case there are multiple matches, you can do:

$ awk '/ActFlg="1"/ {print preprev} {preprev=prev; prev=$0}' file
<task Col="6" Object="ABCD">
<task Col="7" Object="ABCDE">

Explanation

  • '/ActFlg="1"/ {print preprev} matches lines containing ActFlg="1" and does print preprev, a stored line.
  • {preprev=prev; prev=$0} this keeps storing the two previous lines. The 2 before is stored in preprev and the previous in prev. So whenever we are in a new line, this gets updated.
Sign up to request clarification or add additional context in comments.

9 Comments

This is not showing , if the same actflg="1" repeats multiple time
Well I replied based on your sample input. If you change the sample, don't come and downvote...
I did not do that !! Please remember that i am here to ask for help ! anyone's answer is well appreciated even if it is wrong because they help me !!
OK I thought it was your downvote. It is a bit tricky because the way to do it will be awk but having the "ignore case" comparison it is more difficult. Will try to find a moment to figure out how to do it.
Thanks.. even if it is case sensitive its okay.. because its predefined structure file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.