How to grep multiple strings with -A

gedDydaytut

New Member
I need to grep for data contained in an XML file. There are multiple elements I need to get, and the last is inside a node. There is a bunch of data in between the elements I'm grepping through. I can easily grep for the multiple elements like so:\[code\]grep -E "<first|<second|<third|<seventh" file.xml\[/code\]But since I have a file structure that looks like this:\[code\]<first>First</first><second>Second</second><third>Third</third><fourth>Fourth</fourth><fifth>Fifth</fifth><sixth flexible="true"> <low>0.09</low> <high>5.90</high></sixth><seventh flexible="false"> <low>1.82</low> <high>3.14</high></seventh>\[/code\]I'm not getting the numerical data inside the \[code\]<seventh>\[/code\] node (didn't expect to with that command). So I'm trying to use the \[code\]grep -An\[/code\] (after) switch, where "n" is the number of lines to match after the initial match, to get the rest of the seventh node:\[code\]grep -E "<first" -E "<second" -E "<third" -E -A3 "<seventh" file.xml\[/code\]Which would return:\[code\]<first>First</first><second>Second</second><third>Third</third><seventh flexible="false"> <low>1.82</low> <high>3.14</high></seventh>\[/code\]Which I could then massage to get my end result (really only need the "high" data from the seventh node along with it's associated 1st, 2nd, & 3rd strings). However, that's not working, I'm getting the first three elements immediately after \[code\]<first\[/code\] and it ignores the rest of the command.I've also tried:\[code\]grep -E "<first|<second|<third" -E -A3 "<seventh" file.xml\[/code\]Which gives me sort of similar results, but completely ignores the "\[code\]-E -A3 "<seventh\[/code\]" part of the command. Well, I guess not ignores, because the -A3 is still being applied backwards to the previous parts of the command. I understand that you can use post-command switches, but can you control how far back they go?I read through the man page on grep and didn't see how I could chain the command together. I'm using a Mac if that's important, but have easy access to Linux & Windows boxes if need be.How can I get the data I want?
 
Back
Top