Skip to main content
The 2024 Developer Survey results are live! See the results
170 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
  • 83.5k
55 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,922
44 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
39 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.4k
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.7k
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.5k
15 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
  • 141k
15 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

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
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
  • 74.3k
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
  • 245k
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,348
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.5k
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 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,996
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
  • 339k
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
  • 339k
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
  • 81.9k
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

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 ...
roblogic's user avatar
  • 227
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
  • 339k
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.9k
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
  • 81.9k
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
  • 339k
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
7 votes

How to change values in XML file

Using XMLStarlet: xml ed -P \ -u '//argument[@name="protocol"]/@is-required' -v true \ -u '//argument[@name="protocol"]/@default-value' -v tcp \ -u '//argument[@name="port"]/@is-required' ...
Satō Katsura's user avatar
7 votes
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 (//),...
Thor's user avatar
  • 17.3k

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