sed (Stream Editor)


This is "non-interactive", "line-oriented", "multi-purpose" editor which combines the work of filters like "grep" with an editor. It's not necessary to open the file to perform the operations on file data. We can do all the text processing from the command line itself without opening a file. The word "sed" is derived from the word "ed" the Unix editor and it supports the "regular expression" in pattern search. 


Syntax:

sed [options] 'actions' filename






This reads the specified file and standard input if no file specification is given with the command, takes the actions as given and writes to the standard output. So when there will not be any change in the original file specified, please give the following to save the changes in file made by "sed" command, :


sed [options] 'actions' filename >>$$; mv $$ filename


Here "$$" is a buffer holding the changes which is redirected to file to make the changes permanent.


Option to be used:


  • "-n" : This option is used when we use "p" under instruction for printing lines from file.


  • "-e" : It makes us enable to use multiple instructions at one line.


  •  "-f" : It makes us enable to take instruction from a specified file.


Instruction-characters:


  • "p" : print, used for display purpose.


  • "q" : quit, number of lines displayed and quit.


  • "w" : write selected lines to a separate file.


  • "i" : insert text with selected line numbers.


  • "a" : append text with selected line numbers.


  • "c" : change text with selected line numbers.


  • "d" : delete, remove selected text.


  • "s" : Substitute, replace a pattern with another string.




Printing Lines:


Lets take a sample file "player.txt" and perform "sed" commands:


$cat player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues
5     Mishel  Reds
6     Alfred  Blues
7     Gil     Blues
$








Printing Lines using Line Numbers:


Print 3 lines and quit:


$sed '3q' player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
$


Print line numbers 1 to 3:


$sed -n '1,3p' player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
$


Print last line:


$sed -n '$p' player.txt
7     Gil     Blues
$


Print all the lines except line number 1 to 2:


$sed -n '1,2!p' player.txt
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues
5     Mishel  Reds
6     Alfred  Blues
7     Gil     Blues
$


Printing multiple lines from a single command:


$sed -n '1p
> 4,5p
>  $p' player.txt
id    name    team
3     Cris    Blues
4     Rich    Blues
7     Gil     Blues
$
$sed -n -e '1p' -e '4,5p' -e '$p' player.txt
id    name    team
3     Cris    Blues
4     Rich    Blues
7     Gil     Blues
$


Printing lines from another input file "input_file.txt": 


$cat > input_file.txt
1p
4,5p
$p
$

$sed -n -f input_file.txt player.txt
id    name    team
3     Cris    Blues
4     Rich    Blues
7     Gil     Blues
$


Printing Lines using Pattern match:


Print the line where ever the pattern "Cris" is found:


$sed -n '/Cris/p' player.txt
3     Cris    Blues
$


Print the lines from the pattern "Amber" to "Cris":


$sed -n '/Amber/,/Cris/p' player.txt
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
$


Printing lines by matching multiple pattern:


$sed -n '/Amber/p
> /Rich/p
> /Gil/p' player.txt
1     Amber   Blues
4     Rich    Blues
7     Gil     Blues
$
$sed -n -e '/Amber/p' -e '/Rich/p' -e '/Gil/p' player.txt
1     Amber   Blues
4     Rich    Blues
7     Gil     Blues
$







Writing Lines from one file to another:


We are taking "player.txt" as source file and I have created one 0 byte file "output.txt" below as target files to check the result. This file is going to be overwritten for every new sed command below:


$cat player.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues
5     Mishel  Reds
6     Alfred  Blues
7     Gil     Blues
$

​$touch output.txt
$ls -l output.txt

​-rw-r--r--  1 pinku  guest     0 May 16 12:27 output.txt

$


Writing first line from Source to target file:


$sed -n '1w output.txt' player.txt
$cat output.txt
id    name    team
$


Writing line numbers 2 to 4 from source to target file:


$sed -n '2,4w output.txt' player.txt
$cat output.txt
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
$


Writing lines from matching pattern "Amber" to line matching pattern "Rich":


$sed -n '/Amber/,/Rich/w output.txt' player.txt
$cat output.txt
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
4     Rich    Blues
$


Writing lines from matching pattern "Mishel" to last line in file:


$sed -n '/Mishel/,$w output.txt' player.txt
$cat output.txt
5     Mishel  Reds
6     Alfred  Blues
7     Gil     Blues
$

Writing 2 more lines from matched pattern line:  


$sed -n '/Amber/, +2w output.txt' player.txt
$cat output.txt
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
$


Writing two different pattern lines to two different files:


$sed -n -e '/Boby/w output.txt' -e '/Cris/w output2.txt' player.txt

$
$cat output.txt
2     Boby    Reds

$
$cat output2.txt
3     Cris    Blues
$


Similarly, Writing two different pattern lines to the same file:


​$sed -n -e '/Boby/w output.txt' -e '/Cris/w output.txt' player.txt
$cat output.txt
2     Boby    Reds
3     Cris    Blues
$


Writing line number 1 ot 4 to first target file and line number 5 to 8 to second target file from source file:


$sed -n -e '1,4w output.txt' -e '5,8w output2.txt' player.txt
$
$cat output.txt
id    name    team
1     Amber   Blues
2     Boby    Reds
3     Cris    Blues
$
$cat output2.txt
4     Rich    Blues
5     Mishel  Reds
6     Alfred  Blues
7     Gil     Blues
$


Continue with SED -

NEXT->

Cracking IT Interview