Search for a tag and display a message if not found.

Hi All,

I am working with a XML file.

Below is part for the file.

                <Emp:Profile>
                        <Emp:Description>Admin</Emp:Description>
                        <Emp:Id>12347</Emp:Id>
                </Emp:Profile>
                <Emp:Profile>
                        <Emp:Description>Support</Emp:Description>
                        <Emp:Id>2222</Emp:Id>
                </Emp:Profile>
                <Emp:Profile>
                        <Emp:Id>000</Emp:Id>
                </Emp:Profile>

Here Description tag is not available in some cases. In that case i would like to display a hardcoded value. Can you please suggest with how to solve this.

Thanks in advance.

Hello Girish19,

Following may help you in same.

awk '/<Emp:Profile>/ {print $0;getline;if($0 ~ /<Emp:Description>/){print $0;next} else {printf "\t\t\t<Emp:Description>Admin</Emp:Description>" ORS $0 ORS;next}} 1' Input_file

Output will be as follows.

                <Emp:Profile>
                        <Emp:Description>Admin</Emp:Description>
                        <Emp:Id>12347</Emp:Id>
                </Emp:Profile>
                <Emp:Profile>
                        <Emp:Description>Support</Emp:Description>
                        <Emp:Id>2222</Emp:Id>
                </Emp:Profile>
                <Emp:Profile>
                        <Emp:Description>Admin</Emp:Description>
                        <Emp:Id>000</Emp:Id>
                </Emp:Profile>

Similarly if you want to need any other line(you haven't told us which description exactly you need to add) you can replace the BOLD one with the desired one and try.
If you have any queries please do let us know with complete input and complete expected output.

Hope this helps.

Thanks,
R. Singh

1 Like

Hi R.Singh thanks for quick reply. It was throwing error..

awk ' /<Emp:Profile>/ { flag=2 } 
      flag-- == 1 && ! /Description/ { print "                        <Emp:Description>Value</Emp:Description>" } 
      1
'   file

Hello Girish19,

I would guess that you're using a Solaris/SunOS system. On Solaris/SunOS systems, you need to change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk . If this is NOT the case, it is advisable always so show us
errors you are getting don't let us guess, we will be able to help you more on same.

Thanks,
R. Singh

Yes i got it.

Thats amazing. It is working :slight_smile:

If you dont mond can you please explain me what is done in the command..

Hello Girish19,

Glad that it helped you, following is the explaination for same.

awk '/<Emp:Profile>/ {print $0;getline;if($0 ~ /<Emp:Description>/){print $0;next} else {printf "\t\t\t<Emp:Description>Admin</Emp:Description>" ORS $0 ORS;next}} 1' Input_file
/<Emp:Profile>/                           # Searching for String Emp:Profile
print $0;getline                            # When match found then printing that line and going to next line
if($0 ~ /<Emp:Description>/)        # Now checking a conditon if nect line's data is having string <Emp:Description>
print $0;next                               # Print that description line and then skip all next statements by next keyword.
else                                           # When a step above if condition doesn't satisfy then
printf "\t\t\t<Emp:Description>Admin</Emp:Description>" ORS $0 ORS;next  # Print the desired string then ORS(Output record seprator which is a new line by default and current line.)
next                                          # leave all further statements now
1                                               # awk works on condition then statement pattern so making conditin TRUE by printing 1 and then it will perform default action which is PRINT as I haven't mentioned any action there.

Hope this helps.

Thanks,
R. Singh

1 Like

Thanks again sir :slight_smile: