Basic sed magic

Replace a simple string

Example: Change port from 8080 to 9090 in config.ini of a Virgo installation:

sed -i 's/8080/9090/' /virgo/home/configuration/config.ini

Replace a placeholder in a file with a value available as environment variable

Hint: Beware of the other type of quotes to allow the shell to replace the values.

sed -i -r "s/<placeholder>/${ENVIRONMENT_VALUE}/g” planets-configuration.yaml

Add a new item to a multi-line list

Change existing line and append one line after the modified line.

Example: Add JavaSE-1.7 to existing java6-server.profile of a Virgo installation:

sed -i 's/JavaSE-1.6/JavaSE-1.6,\\\n JavaSE-1.7/' /virgo/home/configuration/java6-server.profile.

Remove all lines in a file that match a given pattern and write result into new file

Example: Remove all items containing my pattern and write result in a file named scenario-reduced.data:

sed '/my pattern/d' scenario-alpha.data > scenario-reduced.data

Preprocessing pipeline - filter empty/commented-out lines

First things first, the single steps:

  • Filter empty lines and
  • remove commented out lines.

Filter empty lines

sed '/^$/d' file_with_empty_lines.data

Filter lines with comments

sed '#.*$/d'

Chaining the single steps

Imaging you want to inspect a data file full of JSON documents (one each line) with empty lines and commented out documents…

sed -e '/^$/d' -e '#.*$/d' scenario-alpha.data | jq

More on this How to chain sed append commands? sed + remove “#” and empty lines with one sed command

If your document contains more than a single document:

while read line; do echo $line | jq -r '.field.innerField'; done < scenario-alpha.data