Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com

This Blog is protected by DMCA.com

huuphan.com. Powered by Blogger.

Shell script parse xml in linux

How to use shell script parse xml file. In this tutorial, I will use grep command, awk command and sed command Reading xml file extracting value in linux.

For example xml person file as below

<person>
  <sex>Male</sex>
  <firstname>Huu</firstname>
  <lastname>Phan</lastname>
  <sex>female</sex>
  <firstname>Miranda</firstname>
  <lastname>Kerr</lastname>
</person>
How to extracting value Huu and Miranda  of <firstname> </firstname>

Use grep command

[[email protected] huuphan.com]$ grep -oP '(?<=<firstname>).*(?=</firstname)' person.xml

Use awk command

[[email protected] huuphan.com]$ awk -F "[><]" '/firstname/{print $3}' person.xml

Use sed command

[[email protected] huuphan.com]$ sed -n '/firstname/{s/.*<firstname>//;s/<\/firstname.*//;p;}' person.xml 
The result, Shell script parse xml in linux


post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com