Visit The New, Modern Unix Linux Community

Grepping a word from a .xml file dynamically

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping a word from a .xml file dynamically
# 1  
Grepping a word from a .xml file dynamically


I have the xml file as
 <Head="Test"  Id="3" >
            <Title="mode" >

I have used the code to grep the words "Test" and "mode" as
Head=`cat  file.xml | grep "Head" | awk -F "=" '{print $2}' | awk -F " "  '{print $1}'`
 Tilte=`cat file.xml | grep "Title" | awk -F "=" '{print $2}' | awk -F " "  '{print $1}'`

I get the output as

But i need as

I need to avoid the " " from the output..
Please suggest Smilie

Note: The values of xml tags Head and Title are not constant.. They vary according to the application..

Please help
Thanks in advance
# 2  
I think this is what you need:
$ cat file.xml
 <Head="Test"  Id="3" >
            <Title="mode" >

$ cat
Head=`grep Head file.xml | sed 's/.*Head="\([^"]*\)".*/Head=\1'/`
Title=`grep Title file.xml | sed 's/.*Title="\([^"]*\)".*/Title=\1'/`
echo $Head
echo $Title

$ ./


Another way to get rid of the "" characters, using your existing code, would be to add to the end of your pipeline:
tr -d '"'

# 3  
If your grep is GNU, just grep is enough:

To get the Head:
$ grep -oP '(?<=Head=")[^"]*' file

To get the Title:
$ grep -oP '(?<=Title=")[^"]*' file

# 4  
Its better to use $() compare to back tics `` to get value into variable.
You should not use cat with awk and other programs that accept input

Here is a simple solution with awk:
Head=$(awk -F\" '/Head/ {print $2}' file.xml)
Title=$(awk -F\" '/Title/ {print $2}' file.xml)

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #682
Difficulty: Easy
Steve Job's was the primary founder and majority shareholder of Pixar
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

2. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

3. Shell Programming and Scripting

Grepping a word from a .xml file

Hi I have a xml file vi lpower.xml <head = power_health> Now, I need to grep "power_health" alone from that file using shell.. Please help (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

4. Shell Programming and Scripting

How to find a word and move it a specific location in xml file using perl?

Hi friends, I have one XML file having below structure :- INput XML file :- <?xml version="1.0" encoding="UTF-8"?> <START> <A=value1> <attr name1="a1"> </A> <B=value2> <attr name2="b1"> <attr name3="c1"> </B> </START> output xml file should be === (3 Replies)
Discussion started by: harpal singh
3 Replies

5. Shell Programming and Scripting

Grepping more than one word

Dear Experts, Need your help. Typically we use "grep" to search and display a pattern in a txt file. However, here what we want is, we want to grep a line which contains 4 words any where in a line. For example. File has 10,000,000 lines in it out of which there is a particular line which... (1 Reply)
Discussion started by: anushree.a
1 Replies

6. Shell Programming and Scripting

word counts for a single line xml file

I have any XML ouput file(file name TABLE.xml), where the data is loaded in A SINGLE LINE, I need help in writting a ksh shell script which gives me the word counts of word <TABLE-ROW> This is my input file. <?xml version="1.0" encoding="UTF-8"?><!--Generated by Ascential Software... (4 Replies)
Discussion started by: pred55
4 Replies

7. Shell Programming and Scripting

Grepping word based on white space.....

Hi, I am having a text file with following contents: word word I want to grep the first line i.e. word that is being preceded with three space characters. So i tried sed -n '/ {3}/p' filename grep " {3}" filename But is not returning any result. If i don't use {}, then it... (5 Replies)
Discussion started by: sarbjit
5 Replies

8. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

9. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies

10. Shell Programming and Scripting

BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)

I have a file that lists data about a system. It has a part that can look like: the errors I'm looking for with other errors: Alerts Password Incorrect Login Error Another Error Another Error 2 Other Info or, just the errors I need to parse for: Alerts Password Incorrect ... (9 Replies)
Discussion started by: elinenbe
9 Replies

Featured Tech Videos