Skip to content

SED examples

Using back references to append text to each line

Section titled “Using back references to append text to each line”

To appending the string EXTRA TEXT to each line

Terminal window
sed -e 's/\(.*\)/\1EXTRA TEXT/'

Note the backslash before the brackets.

\1 is the back reference to the first group (only 1 group in this case, which has matched the whole line).

Delete all occurrences of 4 consecutive digits

Section titled “Delete all occurrences of 4 consecutive digits”
Terminal window
sed -e 's/[0-9]\{4\}//g'
Terminal window
sed -e 's/\(.*\)\s\(.*\)/\2 \1/'

Use the -r switch to turn on extended regular expression. This allows you to use symbols such as ? for zero or one match.

Terminal window
$ echo abcd | sed -e 's/ab?//'
abcd
Terminal window
$ echo abcd | sed -r -e 's/ab?//'
cd

SED one liners