{"id":22046,"date":"2024-03-15T22:57:56","date_gmt":"2024-03-15T22:57:56","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/"},"modified":"2024-03-21T21:56:21","modified_gmt":"2024-03-21T21:56:21","slug":"how-does-the-shell-read-the-attribute-values-of-xml-nodes","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/","title":{"rendered":"How does the shell read the attribute values of XML nodes?"},"content":{"rendered":"<p>One way to read the attribute values of XML nodes in Shell is to use command line tools like sed and awk. Here is a method using the sed command.<\/p>\n<p>Assume we have an XML file named example.xml, which contains the following content:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">root<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">node<\/span> <span class=\"hljs-attr\">attribute<\/span>=<span class=\"hljs-string\">\"value1\"<\/span>\/&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">node<\/span> <span class=\"hljs-attr\">attribute<\/span>=<span class=\"hljs-string\">\"value2\"<\/span>\/&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">root<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<p>To retrieve the attribute value of a node, you can use the following command:<\/p>\n<pre class=\"post-pre\"><code>attribute_value=$(sed -n <span class=\"hljs-string\">'s\/.*&lt;node attribute=\"\\([^\"]*\\)\".*\/\\1\/p'<\/span> example.xml)\r\n<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-variable\">$attribute_value<\/span>\r\n<\/code><\/pre>\n<p>The output is:<\/p>\n<pre class=\"post-pre\"><code>value1\r\nvalue2\r\n<\/code><\/pre>\n<p>This command uses sed&#8217;s regular expressions to match and extract the value of the attribute attribute. Specifically, .*<node attribute=\" matches lines that start with <node attribute=\"; [^\"]* matches any character that is not a double quote; \".*\/ matches all characters after the double quote until the end of the line. By referencing the matched attribute value using \\1, the sed command will only output the matched attribute value.<\/p>\n<p>The above command reads the attribute values of all node nodes and stores them in a variable named attribute_value. To only read the attribute value of the first node node, you can use the following command:<\/p>\n<pre class=\"post-pre\"><code>attribute_value=$(sed -n <span class=\"hljs-string\">'0,\/&lt;node attribute=\"\\([^\"]*\\)\"\/ s\/.*&lt;node attribute=\"\\([^\"]*\\)\".*\/\\1\/p'<\/span> example.xml)\r\n<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-variable\">$attribute_value<\/span>\r\n<\/code><\/pre>\n<p>The output result is:<\/p>\n<pre class=\"post-pre\"><code>value1\r\n<\/code><\/pre>\n<p>Limit the sed command to only match the attribute value of the first node node by adding 0,\/<node attribute=\"\\([^\"]*\\)\"\/ here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One way to read the attribute values of XML nodes in Shell is to use command line tools like sed and awk. Here is a method using the sed command. Assume we have an XML file named example.xml, which contains the following content: &lt;root&gt; &lt;node attribute=&#8221;value1&#8243;\/&gt; &lt;node attribute=&#8221;value2&#8243;\/&gt; &lt;\/root&gt; To retrieve the attribute value of [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-22046","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How does the shell read the attribute values of XML nodes? - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How does the shell read the attribute values of XML nodes?\" \/>\n<meta property=\"og:description\" content=\"One way to read the attribute values of XML nodes in Shell is to use command line tools like sed and awk. Here is a method using the sed command. Assume we have an XML file named example.xml, which contains the following content: &lt;root&gt; &lt;node attribute=&quot;value1&quot;\/&gt; &lt;node attribute=&quot;value2&quot;\/&gt; &lt;\/root&gt; To retrieve the attribute value of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T22:57:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T21:56:21+00:00\" \/>\n<meta name=\"author\" content=\"Isabella Edwards\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Isabella Edwards\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/\"},\"author\":{\"name\":\"Isabella Edwards\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\"},\"headline\":\"How does the shell read the attribute values of XML nodes?\",\"datePublished\":\"2024-03-15T22:57:56+00:00\",\"dateModified\":\"2024-03-21T21:56:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/\"},\"wordCount\":90,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/\",\"name\":\"How does the shell read the attribute values of XML nodes? - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T22:57:56+00:00\",\"dateModified\":\"2024-03-21T21:56:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How does the shell read the attribute values of XML nodes?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd\",\"name\":\"Isabella Edwards\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g\",\"caption\":\"Isabella Edwards\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How does the shell read the attribute values of XML nodes? - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/","og_locale":"en_US","og_type":"article","og_title":"How does the shell read the attribute values of XML nodes?","og_description":"One way to read the attribute values of XML nodes in Shell is to use command line tools like sed and awk. Here is a method using the sed command. Assume we have an XML file named example.xml, which contains the following content: &lt;root&gt; &lt;node attribute=\"value1\"\/&gt; &lt;node attribute=\"value2\"\/&gt; &lt;\/root&gt; To retrieve the attribute value of [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T22:57:56+00:00","article_modified_time":"2024-03-21T21:56:21+00:00","author":"Isabella Edwards","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Isabella Edwards","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/"},"author":{"name":"Isabella Edwards","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd"},"headline":"How does the shell read the attribute values of XML nodes?","datePublished":"2024-03-15T22:57:56+00:00","dateModified":"2024-03-21T21:56:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/"},"wordCount":90,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/","url":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/","name":"How does the shell read the attribute values of XML nodes? - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T22:57:56+00:00","dateModified":"2024-03-21T21:56:21+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-shell-read-the-attribute-values-of-xml-nodes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How does the shell read the attribute values of XML nodes?"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/5579144e23c225c8188167f3e3f888dd","name":"Isabella Edwards","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d4d4dec47f553ac7961d9fa4cc9bdcdcf5b7ce5106594330b6d25c5694fdbaec?s=96&d=mm&r=g","caption":"Isabella Edwards"},"url":"https:\/\/www.silicloud.com\/blog\/author\/isabellaedwards\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/22046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=22046"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/22046\/revisions"}],"predecessor-version":[{"id":55944,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/22046\/revisions\/55944"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=22046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=22046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=22046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}