By: - Laxmikant Chandra
Sed: - stand for “stream editor”. Sed is a multipurpose tool which combines the work of several filters. It is derived from ed, the original UNIX editor. Sed performs noninteractive operation on data stream. It uses very few features but has host of features.
Like diff commandsed uses instruction to act on text. Some examples of sed command are as follow
Suppose we have a file named as name.lst containing following data
01|laxmikant chandra |student |christ university
02|Shishir kumar |student |christ university
03|Rohan sharma |emplyee |infosys
04|Makrand ballal |student |christ university
05|laxminarayan |student |christ university
06|john |emplyee |wipro
07|kareem |student |christ university
08|frenk |employee |infosys
09|sophia |student |christ university
10|rocky |student |prsu
I am performing all the commands on the above file.
1.Line addressing:- Here we can use the sed command same as head or tail command to select the first or last lines.
Examples: -
$ sed ‘3q’ name.lst
The above command will return first 3 lines from the name.lst file.
Output:-
01|laxmikant chandra |student |christ university
02|Shishir kumar |student |christ university
03|Rohan sharma |emplyee |infosys
$ sed –n ‘$p’ name.lst
The above command will return last line from the name.lst file.
Output:-
10|rocky |student |prsu
$ sed –n ‘4,9p’ name.lst
The above command will return all the lines from line number 4 to 9.
Output:-
04|Makrand ballal |studdent |christ university
05|laxminarayan |studdent |christ university
06|john |emplyee |wipro
07|kareem |studdent |christ university
08|frenk |employee |infosys
09|sophia |student |christ university
2. Context addressing:- lets specify some patterns to locate lines. The pattern must be bounded by a /.When you specify a single pattern, all lines containing the pattern are selected. It works same as grep command.
Examples: -
$ sed –n ‘/student/p’ name.lst
This command will select all the lines containing student word.
Output:-
01|laxmikant chandra |student |christ university
02|Shishir kumar |student |christ university
04|Makrand ballal |student |christ university
05|laxminarayan |student |christ university
07|kareem |student |christ university
9|sophia |student |christ university
10|rocky |student |prsu
3. Substitution: - Here we can replace the existing pattern in the file with a new pattern.
Examples: -
$ sed ‘s/|/:/’ name.lst | head –n 2
This command will replace all the ‘|’ with ‘:’ and will select first 2 lines from file name.lst.
Output:-
01:laxmikant chandra |student |christ university
02:Shishir kumar |student |christ university
03:Rohan sharma |emplyee |infosys
04:Makrand ballal |student |christ university
05:laxminarayan |student |christ university
06:john |emplyee |wipro
07:kareem |student |christ university
08:frenk |employee |infosys
09:sophia |student |christ university
10:rocky |student |prsu
$ sed ‘1,5s/student/employee/’ name.lst
The above command will replace ‘student’ with ‘employee’ in first 5 lines in file name.lst.
Output:-
01|laxmikant chandra |employee |christ university
02|Shishir kumar |employee |christ university
03|Rohan sharma |emplyee |infosys
04|Makrand ballal |employee |christ university
05|laxminarayan |employee |christ university
06|john |emplyee |wipro
07|kareem |student |christ university
08|frenk |employee |infosys
09|sophia |student |christ university
10|rocky |student |prsu
$ sed ‘s/^/L/’ name.lst | head -2
This command will add ‘L’ at the staring of each line and will select first 2 lines form the file name.lst.
Output:-
L01|laxmikant chandra |student |christ university
L02|Shishir kumar |student |christ university
4. Deleting lines: - using sed we can delete specific lines.
Examples: -
$ sed ‘1d’ name.lst
This command will delete the 1st line from name.lst file.
Output:-
02|Shishir kumar |student |christ university
03|Rohan sharma |emplyee |infosys
04|Makrand ballal |student |christ university
05|laxminarayan |student |christ university
06|john |emplyee |wipro
$ sed ‘d’ name.lst
It will delete all the lines from name.lst.
Output:-
5. Inserting lines: - We can insert new lines before specific line.
Examples: -
$ sed ‘/01/i\NAME LIST’ name.lst | head -5
The above command will insert ‘Hello’ just before the line starting with ‘bat’ in uniq.sh file and will select first 5 lines.
Output:-
NAME LIST
01|laxmikant chandra |student |christ university
02|Shishir kumar |student |christ university
03|Rohan sharma |emplyee |infosys
04|Makrand ballal |student |christ university
No comments:
Post a Comment