sed

Basic Usage #

Stream editor for filtering and transforming text

[me@linuxbox ~]$ echo "front" | sed 's/front/back/'
back

sed will accept any character that follows the character as the delimiter.

[me@linuxbox ~]$ echo "front" | sed 's_front_back_'
back~

Most commands in sed can be preceded by an address, specifying which lines of input streams are to be edited.

[me@linuxbox ~]$ echo "front" | sed '1s/front/back/'
back

Address Notation #

Address Description
n A line number where n is a positive integer
$ The last line.
regexp Lines matching a POSIX basic regular expresion.
Note that the regex is delimited by slash characters.
Optionally, the regex may be delimited by an alternate char,
by specifying the expression with , where c is alternate.
addr1,addr2 A range of lines from addr1 to addr2, inclusive
first~step Match the line represented by the number first, then each
subsequent line at step intervals.
addr1,+n Match addr1 and the following n lines.
addr! Match all lines except addr, which may be any of the forms listed earlier

sed Address Notation

Editing Commands #

Command Description
= Output the current line number
a Append text after the current line
d Delete the current line
i Insert text in front of the current line
p Print the current line. By default, sed prints every line and
only edits lines that match specified address within the file
q Exit sed without processing any more lines. If the -n option
is not specified, output the current line.
Q Exit sed without processing any more lines
s/regex/replacement/ Substitute the contents of replacement wherever regex
y/set1/set2 Transliterate by converting chars from set1 to chars in set2

sed Basic Editing Commands

Substitution #

sed s/day/night <oldfile >newfile sed s/day/night oldfile >newfile These both do the same thing

echo day | sed s/day/night/

Best practice is to use quotes in case there are meta-characters in the command. sed ’s/day/night/' <old >new

Delimiters #

You can use anything as a delimiter as long as it’s not in the string your trying to substitute.

/ and _ are the most commonly used

sed ’s/\/usr\/local\/bin/\/common\/bin/' <old >new sed ’s_/usr/local/bin_/common/bin_' <old >new

Some others are : and |

sed ’s:/usr/local/bin:/common/bin:' <old >new sed ’s|/usr/local/bin|/common/bin|' <old >new

Using ‘&’ for Matched String #

sed ’s/[a-z]*/(&)/' <old >new this puts parentheses around the matched string

Extended Regular Expressions #

sed -r echo “123 abc” | sed -r ’s/[0-9]+/& &/' Otherwise can’t use the ‘+'

Back-References #

Keep the first word and remove the rest. sed ’s/([a-z]*).*/\1/’

echo abcd123 | sed ’s/([a-z]*).*/\1/' This will output “abcd” and delete the numbers.

Can rearrange order of things. sed -r ’s/([a-z]+) ([a-z]+)/\2 \1/'

Can detect duplicated words. sed -rn ‘/([a-z]+) \1/p’ This will print lines containing duplicated words

You can have up to 9 back-references. Reverse the first 3 characters in a line. sed ’s/^(.)(.)(.)/\3\2\1/'

Pattern Flags #

Flag Description
/g Replace all occurrences on a line
/

Sources: #

https://www.thegeekstuff.com/2009/10/unix-sed-tutorial-advanced-sed-substitution-examples/ https://stackoverflow.com/questions/2232200/regular-expression-in-sed-for-masking-credit-card https://www.grymoire.com/Unix/Sed.html http://tldp.org/LDP/abs/html/x23170.html https://www.folkstalk.com/2012/01/sed-command-in-unix-examples.html

TODO Sed Introduction: https://www.grymoire.com/Unix/Sed.html #

Time Tracking #