How to grep the particular string?

Hi,
I have one xml file. In that file, I have <name> and <description> . I need this name and description details alone and put it in one file.
Please find the example below:

<name>This is a file name</name>
<description>Latest Release 1.0.2.0</description>

The above tags are come in the xml file. So I need to create file which contains "This is a file name" and "Latest Release 1.0.2.0" alone.
Please suggest how to do it..

Thanks

Chella..

sed is the tool for this:

sed -e 's/<name>\(.*\)<\/name>/\1/g' -e 's/<description>\(.*\)<\/description>/\1/g' file.xml > newfile.txt
1 Like

For simple tasks, these may be fine.

But for more XML parsing, go for a scripting language.

Thanks for your quick reply. Ya. I need this activity for multiple files... How can we implement this in script???

---------- Post updated at 06:09 PM ---------- Previous update was at 06:05 PM ----------

And , if i use the above command, i will get the entire xml file. Because in that file, i have some other details are tagged in that. Could you please advice me how to get only those two details???

xmlint can do the job. Of course a high level scripting language like perl, python can do a much better job

$ cat a.xml
<?xml version="1.0"?>
<doc>
<name>
Doc Name
from a.xml
</name>
<description>
Doc Description
from a.xml
</description>
<others>
Other info
</others>
</doc>

$ name=`echo "xpath //name/text()" | xmllint --shell a.xml | sed -n 's/.*content= \(.*\)/\1/p'`

$ description=`echo "xpath //description/text()" | xmllint --shell a.xml | sed -n 's/.*content= \(.*\)/\1/p'`

$ echo $name
Doc Name from a.xml

$ echo $description 
Doc Description from a.xml

Just use a for loop to do for multiple files, I will leave this to you to finish off the rest of the script.