Friday, April 2, 2010

All about sed - stream editor

Sed is used to do substitution (append, replace, insert, delete, substitute,
translate, print, copy, paste,…etc)   in files and create a new file w/o changing the first file.

sed 's/pat1/pat2/g' infile outfile  --> substitutes pat1 with pat2 in infile and creates a new file outfile.

By default sed prints each line at the std out.

Sed at command line will have below options

sed  [-n] –f script file file(s)
-or-
sed  [-n] [–e] ‘command’  files(s)

sed -f   script     => -f  indicates a sed script is given in command line
sed -n                => doesn't print the output in std out
sed -e                => when multiple sed commands are given this option is used

Some Commands
d delete line  
p print line   
h copy to temp buffer
g paste (replace with temp buffer)

EXAMPLES

Lines containing a pattern can be deleted by
sed -n ’/rexp/d’
sed -n  '/rexp/!d'  this command deletes thelines not containing the pattern

sed -n '/[0-9]/p' prints lines that contain any digits.

 sed ‘1,5/d’ somefile
prints everything but the first 5 lines of the file somefile
(same as tail +6 somefile).

> sed –n ‘/foo/p’ /usr/dict/words
just like grep foo /usr/dict/words

> sed –n ‘/START/,/STOP/p’
prints everything from STDIN between
a line that contains START and one the contains STOP

substitution examples
sed ‘s/[wW]indows/Unix/g’   substitues windows or Windows with Unix


For example, to substitute "1" with "2" only in the fifth and sixth lines of the sample file's output, the command would be:
$ sed '5,6 s/1/2/' myfile.txt



Suppose that it would be desirable for "1" to be substituted with "2," but only after the word "two" and not throughout every line. This can be accomplished by specifying that a match is to be found before giving the substitute command:
$ sed '/two/ s/1/2/' myfile.txt



The up carat (^) signifies the beginning of a line, thus
sed '/^two/ d' myfile.txt
would only delete the line if "two" were the first three characters of the line.
·  The dollar sign ($) represents the end of the file, or the end of a line, thus
sed '/two$/ d' myfile.txt
would delete the line only if "two" were the last three characters of the line.
The result of putting these two together:
sed '/^$/ d' {filename}
deletes all blank lines from a file. For example, the following substitutes "1" for "2" as well as "1" for "3" and removes any trailing lines in the file:
$ sed '/two/ s/1/2/; /three/ s/1/3/; /^$/ d' myfile.txt
one     1
two     1
three   1
one     1
two     2
two     2
three   1
$
A common use for this is to delete a header. The following command will delete all lines in a file, from the first line through to the first blank line:
sed '1,/^$/ d' {filename}


TRY these in off

• Line numbering
– Line numbers can be used to select a range of lines over which the commands will operate
– Examples
$ sed -n ’20,30p’
$ sed ’1,10d’
$ sed ’1,/ˆ$/d’
$ sed -n ’/ˆ$/,/ˆend/p’
– sed does not support relative line numbers (difference with respect to ed)


Command to remove the mail header from a saved mail message
sed  ’1,/ˆ$/d’ in_file > out_file




No comments: