ameyrk
February 15, 2012, 5:38pm
1
Respected All,
I have a very big xml in that i want to search only below 3 lines.
<target name ="UpgradePrimaryBox" depends ="configureBox1">
<echo> Finished Upgrading Primary Box </echo>
</target>
grep -i "<target.*UpgradePrimaryBox" this gives me the first line.
then i need to match second line and the </target>.
Please help me.
Thanks,
Amey
birei
February 15, 2012, 6:13pm
2
Hi ameyrk,
There is an option that print following lines of a match. I don't know if your version support it. Try it:
$ grep -A2 -i "<target.*UpgradePrimaryBox"
Regards,
Birei
ameyrk
February 15, 2012, 6:34pm
3
Birei,
correct my version does not support.
my question is like grep -i "<target.*UpgradePrimaryBox<followed by new line><then followed by target>"
any help?
grep matches lines. If what you want isn't a matching line, it doesn't really do it. It's not a programming language, you can't tell it "if x, print two more lines."
awk is a programming language however. You can tell it "if x, print 2 more lines".
awk '/<target.*UpgradePrimaryBox/ { print; getline; print; getline ; print }' file
ameyrk
February 15, 2012, 7:01pm
5
Wow that really worked. In this case we know there are 3 lines but if the opening and closing target are really big then any idea on that?
thanks Corona
If it's too big for awk on your sytem try nawk.
Solution to post #5:
sed -n '/<target.*UpgradePrimaryBox/,/<\/target>/p' inputfile
$ nawk '/\<target/ , /\<\/target>/' input.txt
<target name ="UpgradePrimaryBox" depends ="configureBox1">
<echo> Finished Upgrading Primary Box </echo>
</target>