Howard Paget
Projects
About

Command Line: sed

Feb 20, 2022
command-line

sed is a handy tool to find patterns and make substitutions in the contents of files. In this article we’ll go through some common use cases.

Sample File

For the examples below we’ll use a file with the following as contents to demonstrate the result of the sed examples:

items:
  thing-1
  thing-2
dates:
  01-01-2022
  15-04-2022
  18-04-2022

Find and Replace

A straight forward use of sed is to find a string “thing” in a file and replace it with something else “item”.

sed 's/thing/item/g' file
  • s: perform a substitution.
  • /thing/item/: find ’thing’ and replace with ‘item’.
  • g: apply globally i.e. to multiple matches in the line.
  • Note: -i can be used to make the changes in place i.e. update the file rather than print to stdout.

Result:

items:
  item-1
  item-2
dates:
  01-01-2022
  15-04-2022
  18-04-2022

Find and Replace with Captured Groups

Brackets e.g. (regex) can be used to capture a regex and provide the matches to the replacement as \1, \2, …. The example below converts DD-MM-YYYY to YYYY-MM-DD by matching ##-##-#### and substituting with the groups in reverse.

sed -E 's/([0-9]{2})-([0-9]{2})-([0-9]{4})/\3-\2-\1/g' file
  • -E: use extended regex which is much clearer than “basic” regex which require backslashes before brackets.

Result:

items:
  thing-1
  thing-2
dates:
  2022-01-01
  2022-04-15
  2022-04-18

Filter Lines

We can selectively print lines by preventing sed from printing using -n and then only printing matches with p.

sed -n -E 's/[0-9]{2}-[0-9]{2}-[0-9]{4}/\0/p' file
  • -n: Don’t print lines by default.
  • p: Print the substitution in this case \0 which is just the whole line.
  01-01-2022
  15-04-2022
  18-04-2022

Further Reading