{"id":252,"date":"2010-05-03T06:40:34","date_gmt":"2010-05-03T06:40:34","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2010\/05\/03\/reading-xml-file-using-shell-script\/"},"modified":"2017-12-27T20:25:20","modified_gmt":"2017-12-27T20:25:20","slug":"reading-xml-file-using-shell-script","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/reading-xml-file-using-shell-script\/","title":{"rendered":"How to read XML file by using shell script ?"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4367 aligncenter\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2010\/05\/xml-file-using-shell-script.png\" alt=\"read-xml-file-using-shell-script\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/05\/xml-file-using-shell-script.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/05\/xml-file-using-shell-script-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>This was like the first time where I had to write something that will be able to read something out of a XML file using a shell script. Usually I would use Python\/Perl as my favorite choices in such a scenario but in this one I really *had* to do all within a shell script.<\/p>\n<p>This is an example of the type of XML file I had to read:<\/p>\n<p><code>Fixed a new bug<br \/>\nShoaib Mir<br \/>\nSun, 02 May 2010<br \/>\nshoaibmir[@]gmail.com<\/code><\/p>\n<p>I ended up having a shell script like this:<br \/>\n<code><br \/>\n#!\/bin\/bash<\/code><\/p>\n<p><code>#Looking for four keywords in here<br \/>\nfor key in changelog name date email<br \/>\ndo<br \/>\nOUTPT=`grep  $key log.xml | tr -d '\\t' | sed 's\/^\\([^&lt;].*\\)$\/\\1\/' `<br \/>\neval ${key}=`echo -ne \\\"\"${OUTPT}\"\\\"`<br \/>\ndone<\/code><\/p>\n<p># Getting the results in four specific arrays<br \/>\nchangelogarr=( `echo ${changelog}` )<br \/>\nnamearr=( `echo ${name}` )<br \/>\ndatearr=( `echo ${date}` )<br \/>\nemailarr=( `echo ${email}` )<\/p>\n<p><code>#Print all Arrays<br \/>\necho ${changelogarr[@]}<br \/>\necho ${namearr[@]}<br \/>\necho ${datearr[@]}<br \/>\necho ${emailarr[@]}<br \/>\n<\/code><\/p>\n<p>Which gives me an output:<br \/>\n<code><br \/>\nshoaib@shoaib-desktop:~\/Desktop$ .\/readxml.sh<br \/>\nFixed a new bug Shoaib Mir<br \/>\nSun, 02 May 2010<br \/>\nshoaibmir[@]gmail.com<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><b>Reference:<\/b><\/p>\n<p><a href=\"http:\/\/shoaibmir.wordpress.com\/2010\/05\/02\/reading-xml-file-using-shell-scripting\/\" target=\"_blank\" rel=\"noopener\">Reading XML file using shell&nbsp;script<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This was like the first time where I had to write something that will be able to read something out of a XML file using a shell script. Usually I&#8230; <\/p>\n","protected":false},"author":1,"featured_media":4367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[349,1122,3113,1806,593,4115,4116,4120,4118,278,138,3046,4119,1927,524,4117],"class_list":["post-252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-shell-script","tag-file","tag-guide","tag-how-to","tag-instruction","tag-process","tag-read","tag-read-xml-file","tag-read-xml-file-using-shell-script","tag-read-xml-with-shell-script","tag-script","tag-shell","tag-shell-script","tag-shell-script-for-reading-xml-content","tag-using","tag-xml","tag-xml-file-reading-by-shell-script"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":4368,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions\/4368"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/4367"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}