Stream editor sed
sed is a powerful tool to filter and transform text. We see some examples of sed commands:
sed '1d' filename: deletes first line;
sed '1,3d' filename: deletes first three lines;
sed -n '1p' filename: prints first line (
sednot to print lines unless they match the following commands, in this case
1pthat prints the first line);
sed 's/dino/mino/' filename: substitutes the first occurence of
minofor each line. Adding
gat the end (global) has the effect of applying substitution to any occurrences of
imakes the search case insensitive.
A number indicates which occurrence should be replaced. For example,
sed 's/ciao/hello/i2'is case insensitive and replaces the second occurrence of
hellofor each line;
- Notice that it is possible to use a custom separator with subsitution command. For example,
sed 's:dino:mino:' filename
- It is possible to indicate which rows should be examined as in
sed '6,7s:dino:mino:' filenamewhich only operates from row 6 to row 7;
- In order to only print rows that match a string
dinoit is possible to use
sed -n '/dino/p' filename.
Analogously it is possible to use
dto delete rows that match (in this case without
sed 'y/abc/ABC/'replaces each occurrence of a,b and c with A, B and C, respectively.
Regular expressions in sed
sed supports regular expressions. For substitutions, it is useful to refer to the matched text. This can be done in different ways:
&it is possible to perform find-and-replace:
&is substituted with the matched string. For example, in order to add
worldwe can use
sed 's/world/hello &/g';
- It is possible to refer to matched substring using back references and brackets. For example, to extract the name from a letter we can use
sed 's/Dear \([^ ]*\) .*$/Name = \1/g'. Notice that the pattern we refer to is surrounded by
\), while the reference to it is
\1. If we have more than one substring we can use
\3, and so on.
Given a list of telephone numbers of the form
sed to rewrite them as
$ cat numeri.txt
Not a number
$ cat numeri.txt | sed ...
Not a number
sed to select and convert all file names with suffix
.html given as output by
ls into capital letters with suffix
.HTM. Check out command
sed man page. An example of the expected output follows:
$ ls | sed ...
sed to extract full user names from
/etc/passwd/. For example:
$ sed ... /etc/passwd
NVIDIA Persistence Daemon
Try to solve as many levels as possible of the bandit wargame.
NOTE: This page has been converted from asciidoc html to wordpress html using:
sed '1,/<body/d; /<\/body/,$d; s/<\/*body[^>]*>//g; s/<\/*div[^>]*>//g; s/<\/*p>//g; s/<pre><code>/<pre lang="bash">/g; s/<\/code><\/pre>/<\/pre>/g; s/^Last updated .*//g; /^\s*$/d' shell-english.html > shell.wordpress.html