45votes
Accepted
Extract an attribute value from XML
Use an XML parser for parsing XML data. With xmlstarlet it just becomes an XPath exercise: $ branch=$(xmlstarlet sel -t -v '//blah1[@name="andy"]/@branch' file.xml) $ echo $branch master
18votes
Extract an attribute value from XML
With grep: grep -Pio 'name="andy".*branch="\K[^"]*' file -P enable perl regular expressions (PCRE) -i ignore case -o print only matched parts In the regex, the \K is a zero-width lookbehind to match ...
17votes
Extracting sea level pressure from KML using Shell
I suggest to use a tool that can handle XML correctly: xmlstarlet select --template --value-of '//minSeaLevelPres' -n weatherdata.kml Output: 1002 See: xmlstarlet select --help
15votes
Accepted
How to remove nodes from XML file as command line with namespace?
TL;DR please, never ever use sed for this task ! Everytime you use sed for html or xml, you kill a kitty It's a task for xmlstarlet (a proper XML parser) and his friend xpath, like this: xmlstarlet ...
14votes
Parse XML to get node value in bash script?
Using xmllint and the --xpath option, it is very easy. You can simply do this: XML_FILE=/path/to/file.xml HOST=$(xmllint --xpath 'string(/config/global/resources/default_setup/connection/host)' $...
14votes
Accepted
$1 not working with sed
sed backreferences have the form \1, \2, etc. $1 is more Perl-like. Also, if using basic regular expressions (BRE), you need to escape the parentheses (...) forming a group, as well as ? and +. Or you ...
14votes
Shell script to remove child xml tags conditionally
Although possible, it is a very, very bad idea to attempt to parse XML or HTML with tools like sed that are based on regular expressions. That can work for simple cases but gets really hard to get ...
13votes
How to find a specific tag section in an XML file?
Since your given example is a valid XML file, so I would use xq XML parser tool for that which is part of the yq installation package. xq -x --xml-root key ' .schemalist.schema.key[] | select(.&...
12votes
Extract an attribute value from XML
Use xmllint to extract the value of the attribute using XPath: xmllint --xpath 'string(/blah/blah1[@name="andy"]/@branch)' file.xml It's better to use an XML parser to process XML since the order of ...
10votes
Accepted
sed on cygwin can only replace one character?
Most probably, that file is encoded in UTF-16, that is with 2 or 4 bytes per characters, probably even with a Byte-Order-Mark at the beginning. The characters that are shown in your sample (all ASCII ...
9votes
Extract the Children of a Specific XML Element Type
If you really want sed- or awk-like command-line processing for XML files then you should probably consider using an XML-processing command-line tool. Here are some of the tools that I've seen more ...
9votes
XML context grepping
If this is part of a well formed XML document you can extract the required part with an XML parser. To satisfy the well formed requirement, I've wrapped your XML fragment with <root> and </...
9votes
XML command line (shell script) manipulation
XMLStarlet (http://xmlstar.sourceforge.net/overview.php) is written in C and uses libxml2 and libxslt. Given the XML document <?xml version="1.0"?> <root> <tag>data</tag> &...
9votes
Accepted
Change XML node's value, using sed?
You would use an XML parser to do this. For example xmlstarlet (a command line XML tool): $ xmlstarlet ed -u '//client-version' -v '1.2.9' file.xml <?xml version="1.0"?> <client-...
9votes
reading XML file and extract only node names and structure
The xmllint interactive shell command du appears to provide what you want: du PATH Show the structure of the subtree under the given path or the current node. If you want something non-...
8votes
how to use patch and diff to merge two files and automatically resolve conflicts
sdiff (1) - side-by-side merge of file differences Use the --output option, this will interactively merge any two files. You use simple commands to select a change or edit a change. You should make ...
8votes
How do I parse namespaces from an XML file using XMLLINT and BASH
Try using a here-doc. Example: #!/bin/bash xmllint --shell file.xml <<EOF setns swid=http://standards.iso.org/iso/19770/-2/2008/schema.xsd xpath //swid:product_version/swid:name/text() EOF ...
8votes
Insert custom XML tag into an XML file in a bash script
With XMLStarlet, you can add a subnode to an element, or you can update an existing element. To cover both the case when the element exists and when it doesn't already exist, we can Delete the ...
8votes
Fetching the string inside single quote
To extract the value of all file attributes of all source nodes in an XML document, you may use xmlstarlet like this: xmlstarlet sel -t -v '//source/@file' -nl file.xml Or, reading from your virsh ...
8votes
How to find a specific tag section in an XML file?
Since you are dealing with valid XML, you can use xmlstarlet: xmlstarlet sel -t -c "/schemalist/schema/key[@name='enabled']" infile.xml This will query (sel) the XML document and print a ...
8votes
Sorting an XML file in UNIX with a Bash script?
[with a generous assist from Kusalananda] You can do it using the xq wrapper from yq (a jq wrapper for YAML/XML) to leverage jq's sorting capabilities: $ xq -x 'getpath([paths(scalars)[0:-1]] | unique ...
8votes
Using sed to replace one character with another within an xml tag
Assuming you have some XML document, like <data> <episode-num system="onscreen">S1 E12</episode-num> <episode-num system="onscreen">S1 S12</episode-num&...
8votes
Can I extract complete dates from file with grep command?
Don't use grep nor regex to parse HTML/XML you cannot, must not parse any structured text like XML/HTML with tools designed to process raw text lines. If you need to process XML/HTML, use an XML/HTML ...
7votes
Accepted
how to massage or format html in order to parse with xmstarlet?
If you just want the table's data cells, it is possible using xmlstarlet fo followed by xmlstarlet sel. The main issue you are having is with the XPath. If you add a couple of "wildcard" elements (//),...
7votes
Accepted
Edit XML using xmlstarlet only in one subnode
xmlstarlet solution: new_branch="DAVID" xmlstarlet ed -u "/configurations/rules/rule[name='APP1']/branch" -v "$new_branch" config.xml > final.xml The crucial <rule> node should look like: &...
7votes
Accepted
Insert some lines before a specific line with sed
Don't use sed, awk and alike for parsing XML/HMTL data - it'll never come to robust and scalable result. Use a proper XML/HTML processors. The right way with xmlstarlet tool: xmlstarlet ed -s '//...
7votes
reading XML file and extract only node names and structure
Since you're already using xmlstarlet you may as well continue using it. The xmlstarlet tool has an el (elements) sub-command which is used to "Display element structure of XML document". By ...
7votes
I need to use sed in Linux to find a portion of a line in XML and update a value in it?
Using xmlstarlet: xmlstarlet ed -u '//Table/@version[ ../@tableName = "Data" and contains(.,"6.02") ]' -v '6.02' file.xml This finds all version attributes of every Table node. ...
7votes
Accepted
sed regex fails to capture the entire paragraph containing the pattern
The reason this doesn't work as you expect is that < and > do not need to be escaped in regular expressions, they don't have any special meaning. However, \< and \> do have special meaning ...
7votes
Accepted
How to rename, in an XML file, specific tags that are embedded in some other specific tags
$ cat file <c>This is <b>an example</b>. <a>This is <b>a test;</b></a></c> $ xmlstarlet ed --rename '//a/b' -v 'd' file <?xml version="1.0&...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
xml × 431sed × 134
text-processing × 102
awk × 65
shell-script × 51
bash × 47
grep × 47
linux × 43
xmlstarlet × 33
regular-expression × 26
xmllint × 25
scripting × 22
shell × 20
perl × 14
command-line × 12
find × 12
replace × 11
python × 10
files × 9
ksh × 8
html × 8
curl × 7
macos × 7
conversion × 7
vim × 6