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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a tag and display a message if not found.
# 1  
Old 02-19-2015
Question 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.

Code:
                <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.
# 2  
Old 02-19-2015
Hello Girish19,

Following may help you in same.
Code:
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.
Code:
                <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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-19-2015
Hi R.Singh thanks for quick reply. It was throwing error..
# 4  
Old 02-19-2015
Code:
awk ' /<Emp:Profile>/ { flag=2 } 
      flag-- == 1 && ! /Description/ { print "                        <Emp:Description>Value</Emp:Description>" } 
      1
'   file

# 5  
Old 02-19-2015
Quote:
Originally Posted by Girish19
Hi R.Singh thanks for quick reply. It was throwing error..
Hello Girish19,

I would guess that you're using a Solaris/SunOS system. On Solaris/SunOS systems, you need to change awkto /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
# 6  
Old 02-19-2015
Yes i got it.

Thats amazing. It is working Smilie

If you dont mond can you please explain me what is done in the command..
# 7  
Old 02-19-2015
Hello Girish19,

Glad that it helped you, following is the explaination for same.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Display multiple words into a single td tag

awk 'BEGIN{print "<table>"} {print "<tr>"; for ( i=1;i<NF;i++) print " <td>" $I "</td>"; print "</tr>"} END{print " </table>"}' <file name > (6 Replies)
Discussion started by: Himanshu1
6 Replies

2. Shell Programming and Scripting

XML files with spaces in the tag name, parse & display?

Greetings all, I have an XML file that is being generated from my application, here is a sample of the first tag (That I am trying to remove and display in a list..) Example- <tag one= "data" data="1234" updateTime="1300"> <tag one= "data1" data="1234" updateTime="1300"> <tag... (5 Replies)
Discussion started by: jeffs42885
5 Replies

3. Shell Programming and Scripting

To search for a particular tag in xml and collate all similar tag values and display them count

I want to basically do the below thing. Suppose there is a tag called object1. I want to display an output for all similar tag values under heading of Object 1 and the count of the xmls. Please help File: <xml><object1>house</object1><object2>child</object2>... (9 Replies)
Discussion started by: srkmish
9 Replies

4. Shell Programming and Scripting

Search for a html tag and print the entire tag

I want to print from <fruits> to </fruits> tag which have <fruit> as mango. Also i want both <fruits> and </fruits> in output. Please help eg. <fruits> <fruit id="111">mango<fruit> . another 20 lines . </fruits> (3 Replies)
Discussion started by: Ashik409
3 Replies

5. Shell Programming and Scripting

not found message

I am trying to execute a script called tfile.sh in a bash shell in solaris and it throws up the following message I am getting the required output after this message. How do i get the message to disappear. Can someone please point out my mistake in the script? Thanks in advance ... (13 Replies)
Discussion started by: goddevil
13 Replies

6. Shell Programming and Scripting

not found message

I am executing the following script in a bash shell in solaris and it throws up the following message : But i get the output that i require nevertheless. Can anyone please spot what is causing the warning and how do i get it go away? VAR1="e6842w2334f76figtl5.systems.grp" if 76fig`... (2 Replies)
Discussion started by: goddevil
2 Replies

7. Shell Programming and Scripting

Search and filter by TAG

Hello all, searching on a text file (log file) is quite simple: grep -i texttosearch filename | grep somethingWhat I'm trying to do is filter the result by TAG and remove the double entries. Like if the log file contains the following text (fields are separated by commas): ... (18 Replies)
Discussion started by: Lord Spectre
18 Replies

8. Shell Programming and Scripting

How to grep for message and if found display filename?

Hi i'm new to the forum and was hoping someone could help me with the following query. I do alot of testing and have hundreds of log files output. I have a script (someone else wrote) which finds all the passed and failed logs and puts a number in a column onto a webpage: e.g: Pass ... (4 Replies)
Discussion started by: defamer
4 Replies

9. UNIX for Dummies Questions & Answers

Unable to extract a tag from a very long XML message

Hi I have a log file which contain XML message. I want to extract the value between the tag : <businessEventId>13201330</businessEventId> i.e., 13201330. I tried the following commands but as the message is very long, unable to do it. Attached is the log file. Please provide inputs. --... (3 Replies)
Discussion started by: Sapna_Sai
3 Replies

10. Shell Programming and Scripting

Search for string and display those NOT found

In my script I read a input file and search all the files in a directory and it's sub-directories for that string using: find . -type f -print | xargs grep $var1 This just displays all the lines the string was found on. Too much data. What I need is to store in a file one time those... (17 Replies)
Discussion started by: John Rihn
17 Replies
Login or Register to Ask a Question