Sed
Contents |
About
sed stands for Stream Editor. It is a Linux/Unix command line program which modifies strings. The command uses so-called regular expressions. It is very often used in shell scripts; e.g. many of the Gentoo portage programmes written in bash use sed. Sed works line-orientated, i.e. it reads the input line by line.
Examples
Example 1: Replacement
This is maybe the most common sed command. s stands for substitute.
| Code: Substitution |
# echo "I hate Linux" | sed 's/hate/love/' I love linux |
This means: In each line, replace the first occurrence of the string "hate" by "love". The echo "I hate Linux" is just to generate some output; the operator "|", a so-called "pipe", redirects this to the sed command.
Example 2: Global replacement
| Code: Global substitution |
# echo "yo yo yo " | sed 's/o/eah/g' yeah yeah yeah |
This means: In each line, replace all occurrences of the string "o" by "eah". Note the g at the end.
Example 3: Regular expressions
The file mails.txt be a text file with a list of names, e-mail adresses and comments in the form John Smith <john.smith@example.com> -- My best friend.
| Code: Regular expressions |
# cat mails.txt | sed -e 's/^.*<//' -e 's/>.*$//' john.smith@example.com ... |
The switch -e allows multiple expressions to be given. The first expression means: Substitute everything from the beginning of each line (^) until the latest occurrence (*.)of the character < with nothing. The second expression means: Substitute everything from the first occurrence of the character > until the end of each line ($) with nothing.
The characters ^, *, . and $ are some members of the large family of regular expressions.
See also
Created by NickStallman.net, Luxury Homes Australia
Real estate agents should list their apartments, townhouses and units in Australia.
