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 :slight_smile:

Hi,

Are these lines always present on the same line number.

Cheers,
Shazin

Hi Shazin, thanks for the answer :slight_smile:

Yes, the lines (if matches the first pattern of <createProfile>), are always in the same position and line number of +3 and +8 from the matching pattern :slight_smile:

Try this:

awk '
/createProfile/{f=1}
f && /msisdn/
f && /contentProfileID/
' file

Pretty cool Franklin52 , that's the way i need the script :slight_smile:

I think that i have to go on with this kind of structure, can you please give me some URLs or references to learn more about this scripts (about the f using) :slight_smile:

Thanks once again :slight_smile:

As i try to understand, the line

/createProfile/{f=1}

makes the first match and if true asigns f=1

the line

f && /msisdn/

makes the second match if the very first if done (if f=1)

and the third line

f && /contentProfileID/

makes the last match if the very first if done too (if f=1)

so, now the question... where is the magic of printing the result?

There's no magic in it, the lines that aren't between braces are evaluated by awk and if they're true awk prints the current line per default.

Regards

thank you for the explanation Franklin52, much more clear now :slight_smile:

best wishes :wink:

---------- Post updated at 11:46 AM ---------- Previous update was at 10:20 AM ----------

As a completion of the script, i've also checked that if there are more than one match of the first pattern (various sets of same code with different values), the script must be modified like

#!/bin/bash

awk '
/createProfile/{f=1}
f && /createProfile/
f && /msisdn/
f && /contentProfileID/
' file.txt

So this will print every set of 3 lines, including the 1st, 2nd and 3rd pattern for every single match of that set in the file :slight_smile:

Thanks once again for the help :wink: