Hot answers tagged

165 votes
Accepted

Replace environment variables in a file with their actual values?

You could use envsubst (part of gnu gettext): envsubst < infile will replace the environment variables in your file with their corresponding value. The variable names must consist solely of ...
don_crissti's user avatar
  • 81.4k
54 votes

Replace environment variables in a file with their actual values?

This is not very nice but it works ( echo "cat <<EOF" ; cat config.xml ; echo EOF ) | sh If it was in a shell script it would look like: #! /bin/sh cat <<EOF <property> <...
hschou's user avatar
  • 2,880
43 votes
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
glenn jackman's user avatar
38 votes
Accepted

How to insert variables inside a string containing ""?

You can embed variables only in double-quoted strings. An easy and safe way to make this work is to break out of the single-quoted string like this: xml='<?xml version="1.0" encoding="iso-8859-1"?...
janos's user avatar
  • 11.3k
17 votes

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 ...
Freddy's user avatar
  • 25.3k
17 votes

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
Cyrus's user avatar
  • 12.1k
14 votes

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)' $...
Dag Wieers's user avatar
14 votes

Replace environment variables in a file with their actual values?

If you happen to have Perl (but not gettext and envsubst) you can do the simple replacement with a short script: $ export INSTANCE_ID=foo; export SERVICE_NAME=bar; $ perl -pe 's/\$([_A-Z]+)/$ENV{$1}/...
ilkkachu's user avatar
  • 135k
14 votes
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 ...
muru's user avatar
  • 71.3k
14 votes
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 ...
Gilles Quénot's user avatar
14 votes

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 ...
terdon's user avatar
  • 239k
13 votes

How to insert variables inside a string containing ""?

Variable expansion doesn't happen in single quote strings. You can use double quotes for your string, and escape the double quotes inside with \. Like this : xml="<?xml version=\"1.0\" encoding=\"...
Sidahmed's user avatar
  • 1,308
13 votes

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(.&...
αғsнιη's user avatar
  • 41.1k
12 votes

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 ...
David Conrad's user avatar
10 votes

Modify multiple lines of an XML file using command line

Use a proper xmltool, in shell, xmlstarlet is a good one : xmlstarlet edit -L -u "//Model500[1]" -v "AAA BBB CCC" file.xml ...
Gilles Quénot's user avatar
10 votes
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 ...
Stéphane Chazelas's user avatar
9 votes

Extract value from XML

Don't use regular expressions to parse XML. Use an XML aware tool, e.g. xmllint: xmllint --xpath 'string(/results/testsuites/testcase/@time)' file.xml
choroba's user avatar
  • 46.4k
9 votes

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 ...
igal's user avatar
  • 9,776
9 votes

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 </...
Chris Davies's user avatar
9 votes

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> &...
Kusalananda's user avatar
  • 327k
9 votes
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-...
Kusalananda's user avatar
  • 327k
9 votes

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-...
steeldriver's user avatar
  • 79.7k
8 votes
Accepted

How to fix "cannot find -lxml2" error while installing 'igraph' for python?

libxml2 is the runtime shared library, suitable for running already-compiled programs that use that library. If you want to compile programs that use libxml2, you need to install libxml2-dev. This ...
cas's user avatar
  • 77.7k
8 votes

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 ...
Cody Allan Taylor's user avatar
8 votes
Accepted

XMLstarlet for JSON?

jq provides a rich expression language for selecting elements from a JSON document. Thus, it is similar to the sel command of xmlstartlet (which takes an XPath expression). For example, to extract a ...
maxschlepzig's user avatar
  • 56.8k
8 votes

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 ...
Kusalananda's user avatar
  • 327k
8 votes

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 ...
AdminBee's user avatar
  • 22.4k
8 votes

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 ...
steeldriver's user avatar
  • 79.7k
8 votes

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&...
Kusalananda's user avatar
  • 327k
8 votes

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 ...
Gilles Quénot's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible