2

I am wondering if there is any way to accomplish this. XML Format:

<Main>
<Master>
<Node1>Content</Node1>
<Node2>qwerty</Node2>
<Node3>854789</Node3>
<Node4>999999</Node4>
</Master>
<Master>
<Node1>Content</Node1>
<Node2>qwerty</Node2>
<Node3>123456</Node3>
<Node4>999999</Node4>
<Node5>qwerty</Node5>
<Node6>123456</Node6>
<Node7>999999</Node7>
</Master>
<Master>
</Master>
<Master>
</Main>

If i use Grep to search for 999999 What i would like, is the whole Node set from to to be printed. But the Nodes can have different number of lines (Its not fixed) One could have 5 the other could have 20 So i cannot use something like: Grep -HrnA10 -B10 "9999999" As this would end up printing other node sets and make viewing the data confusing.

I have thousands of XML Files, so i am querying the files first with find to only pull up the first 30 days worth of files.

example:

find . -name "*.xml" -type f -mtime -30 -exec grep --colour=always -HrnA13 -B20 -E 'Pattern' {} \;

An example of the output, If i searched for "854789" I would like to see this output:

<Master>
<Node1>Content</Node1>
<Node2>qwerty</Node2>
<Node3>854789</Node3>
<Node4>999999</Node4>
</Master>

1 Answer 1

2

If you do not mind using awk:

find -name '*.xml' -type f -exec awk 'BEGIN{RS="</Master>"}/999999/' {} \;

If you want the file name to be outputted too:

find -name '*.xml' -type f -exec awk 'BEGIN{RS="</Master>"}/999999/' {} \; -exec echo -e {}"\n" \;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.