Hi
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
Head="Test"
Tilte="mode"
But i need as
Head=Test
Tilte=mode
I need to avoid the " " from the output..
Please suggest
Note: The values of xml tags Head and Title are not constant.. They vary according to the application..
Please help
Thanks in advance
I think this is what you need:
$ cat file.xml
<Head="Test" Id="3" >
<Title="mode" >
$ cat temp.sh
Head=`grep Head file.xml | sed 's/.*Head="\([^"]*\)".*/Head=\1'/`
Title=`grep Title file.xml | sed 's/.*Title="\([^"]*\)".*/Title=\1'/`
echo $Head
echo $Title
$ ./temp.sh
Head=Test
Title=mode
-------------------------
Another way to get rid of the "" characters, using your existing code, would be to add to the end of your pipeline:
tr -d '"'
If your grep is GNU, just grep is enough:
To get the Head:
$ grep -oP '(?<=Head=")[^"]*' file
Test
To get the Title:
$ grep -oP '(?<=Title=")[^"]*' file
mode
Guru.
Jotne
April 17, 2013, 2:56am
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)