xml - How to grep multiple strings with -A -
I need grep for the data present in an XML file. There are many elements I get, and the last one is inside the node. There is a bunch of data between those elements through which I am focusing on them. I can easily grep for several elements:
grep -E "& lt; first | & lt; second | & lt; third | & lt; seventh" file.xml < / Code> But since I have a file structure that looks like this:
& lt; First & gt; First & lt; / First & gt; & Lt; Second & gt; Second & lt; / Second & gt; & Lt; Third> Third & lt; / Third> & Lt; Fourth & gt; Fourth & lt; / 4th & gt; & Lt; Fifth & gt; Fifth & lt; / Fifth & gt; & Lt; Sixth flexible = "true" & gt; & Lt; Less & gt; 0.09 & lt; / Less> & Lt; High & gt; 5.90 & lt; / High & gt; & Lt; / 6th & gt; & Lt; Seventh Flexible = "False" & gt; & Lt; Less & gt; 1.82 & lt; / Less> & Lt; High & gt; 3.14 & lt; / High & gt; & Lt; / Seventh> me & lt; Seven & gt; Numeric data is not found inside the node (that order was not expected). Therefore, I am trying to use the grep -An (later) switch, where "n" is the number of lines to match after the initial match, to get the remaining seventh node: "E" and the second "-e" and the third "-e-a3" and seventh "file.xml
grep -E" Lt; Who will come back:
& lt; First & gt; First & lt; / First & gt; & Lt; Second & gt; Second & lt; / Second & gt; & Lt; Third> Third & lt; / Third> & Lt; Seventh Flexible = "False" & gt; & Lt; Less & gt; 1.82 & lt; / Less> & Lt; High & gt; 3.14 & lt; / High & gt; & Lt; / Seventh> Then I could massage to get my last result (Actually only need "high" data from the seventh node, with 1, 2, and the third string has been added). However, this is not working, I am getting the first three elements immediately after the first <3> of the code and it ignores the rest of the commands. I have also tried:
grep -E "& lt; first | & lt; second | & lt; third;" -E-A3 "& lt; Seventh "file.xml which gives me the results as well, but completely" -E-A3 "& lt; Seventh "Ignore the part of the order. Okay, I think that is not neglected, because the A3 is still being applied back to the next part of the order. You can use the command switches, but can you control how long they last? I read on grep through the man page and I did not know how I ordered the chain Together. I'm using a Mac If this is important, but easy access to Linux and Windows box is required.
How do I want to get the data?
First of all, please check again through man grep and see that many -E Is not required and many patterns are not expected by grep . Secondly, if you know that you are referring to seventh Required, but not others, just use the two commands: $ grep -E '& lt; First | & Lt; Second. & Lt; Third 'file.xml; Grep-A3 '& lt; Seventh 'file.xml & lt; First & gt; First & lt; / First & gt; & Lt; Second & gt; Second & lt; / Second & gt; & Lt; Third> Third & lt; / Third> & Lt; Seventh Flexible = "False" & gt; & Lt; Less & gt; 1.82 & lt; / Less> & Lt; High & gt; 3.14 & lt; / High & gt; & Lt; / Seventh> If you really do not know, then it would be better to use the xpath or other xml-parsing tool. Also see.
Comments
Post a Comment