help extracting a matching pattern and next lines of match
Hi there,
i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><![CDATA[OK]]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID> <inputGroupID>-2</inputGroupID> <ProfileOmvID>1</ProfileOmvID> <contentID>3365</contentID> <contentProfileID>3525</contentProfileID> <chargingProfileTypeId>22</chargingProfileTypeId> <operationID>201022</operationID> ... i have to test if <createProfile> is in the file. If it does, then i have to extract the lines <msisdn>34661461174</msisdn> and <contentProfileID>3525</contentProfileID> so i've tried staring with something like this > awk '/^<createProfile>/{getline;print}' file.txt but this only print the next line to the matching pattern <createProfile>. With this script > awk '/^<createProfile>/ {print NR,$0}' file.txt i get the line where he regex matches, bu i don't know how to go on to print the registers for <msisdn>34661461174</msisdn> and <contentProfileID>3525</contentProfileID> The file is always this way of structure, i mean all the tags are in the same position if the first matching pattern is matched. Thank you for any help |
You can try with a flag: every time the tag <createProfile> is encountered you switch on the flag. When the tags <msisdn> and <contentProfileID> are encountered they are printed out (or processed). Switch off the flag when it encounters the last tag you need, in the order they appear. E.g. something like:
Code:
/^<createProfile>/ { |
Thanks colucix in another forum i've get this answer that matches my needs
#!/bin/bash awk ' /createProfile/{f=1} f && /createProfile/ f && /msisdn/ f && /contentProfileID/ ' file.txt and could match one that one set of XML :) Thanks you very much for your kind answer :) Best wishes ;) |
Quote:
|
If createProfile always precedes the items you want to extract, you can use sed easily as well:
Code:
sed -n '/createProfile/,${ /msisdn/p |
thank you jschiwal, it's another great solution too :)
i'll take it into account too :D |
For extracting particular items from xml files, look at using xsltproc. That is what it is designed for.
|
Quote:
Code:
... |
Quote:
Code:
<msisdn> Again, if I'm correct, use a true XML parser. |
All times are GMT -5. The time now is 04:59 AM. |