Tuesday, June 15, 2010

SED Linux command

SED Linux Command



sed stands for "stream editor".

SYNOPSIS

% sed [-an] command [file ...]
% sed [-an] [-e command] [-f command_file] [file ...]

DESCRIPTION

sed can be used to filter text files. The pattern to match is typically included between a pair of slashes // and quoted.
For EXAMPLE, to print lines containing the string "1024", you may use:

cat filename | sed -n '/1024/p'

Here, sed filters the output from the cat command. The option "-n" tells sed to block all the incoming lines but those explicitly matching the expression. The sed action on a match is "p"= print.
Here is another EXAMPLE, this time for deleting selected lines:

cat filename | sed '/.*o$/d' > new_file

In this EXAMPLE, lines ending with an "o" will be deleted. It uses a regular expression for matching any string followed by an "o" and the end of the line. The output (i.e., all lines but those ending with "o") is directed to new_file.

To search and replace, use the sed 's' action, which comes in front of two expressions:

cat filename | sed 's/string_old/string_new/' > newfile