Sed (Stream Editor) Command: -
Q) What is the use of sed command ?
Ans – There are three different tasks of sed command.
1.
Printing
2.
Deleting
3.
Substituting
Q) How to print 5th line of a file f1 ?
Ans - $ sed –n ‘5p’ f1<Enter – Key>
Q) How to print 12th to 33rd lines
from a file file.sh ?
Ans - $ sed –n ’12,33p’ file.sh<Enter – Key>
Q) What is the output of the command ?
$ sed –n ’10,5p’ file1<Enter – Key>
Ans – It will print only 10th line of the file
file1
Q) What is the output of the command ?
$ sed –n ’10,+5p’ file1<Enter – Key>
Ans – It will print 10th line and next 5 lines.
Q) What is the output of the command ?
$ sed –n ’1~4p’ file1<Enter – Key>
Ans – It will print 1st line and then skips 2nd,
3rd , 4th lines and print 5th Line, then skips
6th , 7th , 8th lines and print 9th
line and continues till end of the file.
Q) What is the output of the command ?
$ sed –n ’2,5!p’ file1<Enter – Key>
Ans – It will print everything except 2nd to 5th
lines.
Q) What is the output of the command ?
$ sed –n ’2,p’ file1<Enter – Key>
Ans - Error
Q) What is the output of the command ?
$ sed –n ’,5p’ file1<Enter – Key>
Ans – Error
Q) How to print header or 1st line of the file f1
?
Ans - $ sed –n ‘1p’ f1<Enter – Key>
Q) How to print footer or last line of the file f1 ?
Ans - $ sed –n ‘$p’ f1<Enter – Key>
Q) How to print both header and footer of the file f1 in one
command ?
Ans - $ sed –n ‘1p;$p’ f1<Enter – Key>
Q) How to print 10 to 20, 30 to 50 and 43rd lines
of a file f1 ?
Ans - $ sed –n ’10,20p;30,50p;43p’ f1<Enter – Key>
Q) How to temporarily delete 5th line of a file
f1 ?
Ans - $ sed –e ‘5d’ f1<Enter – Key>
Q) How to temporarily delete 12th to 33rd
lines from a file file.sh ?
Ans - $ sed –e ’12,33d’ file.sh<Enter – Key>
Q) What is the output of the command ?
$ sed –e ’10,5d’ file1<Enter – Key>
Ans – It will temporarily delete only 10th line
of the file file1
Q) What is the output of the command ?
$ sed –e ’10,+5d’ file1<Enter – Key>
Ans – It will temporarily delete 10th line and
next 5 lines.
Q) What is the output of the command ?
$ sed –e ’1~4d’ file1<Enter – Key>
Ans – It will temporarily delete 1st line and
then prints 2nd, 3rd , 4th lines and temporarily
delete 5th Line, then prints 6th , 7th , 8th
lines and temporarily delete 9th line and continues till end of the
file.
Q) What is the output of the command ?
$ sed –e ’2,5!d’ file1<Enter – Key>
Ans – It will temporarily delete everything except 2nd
to 5th lines.
Q) What is the output of the command ?
$ sed –e ’2,d’ file1<Enter – Key>
Ans - Error
Q) What is the output of the command ?
$ sed –e ’,5d’ file1<Enter – Key>
Ans – Error
Q) How to temporarily delete header or 1st line
of the file f1 ?
Ans - $ sed –e ‘1d’ f1<Enter – Key>
Q) How to temporarily delete footer or last line of the file
f1 ?
Ans - $ sed –e ‘$d’ f1<Enter – Key>
Q) How to temporarily delete both header and footer of the
file f1 in one command ?
Ans - $ sed –e ‘1d;$d’ f1<Enter – Key>
Q) How to temporarily delete 10 to 20, 30 to 50 and 43rd
lines of a file f1 ?
Ans - $ sed –e ’10,20d;30,50d;43d’ f1<Enter – Key>
Q) How to permanently delete 5th line of a file
f1 ?
Ans - $ sed –i ‘5d’ f1<Enter – Key>
Q) How to permanently delete 15th to 23rd
lines of a file f1 ?
Ans - $ sed –i ’15,23d’ f1<Enter – Key>
Q) How to substitute bad with word good in all positions of
the file f1 ?
Ans - $ sed ‘s/bad/good/g’ f1<Enter – Key>
Where g – globally
Q) How to substitute bad with good word from 3rd
to last field of file f1 ?
Ans - $ sed ‘s/bad/good/3g’ f1<Enter – Key>
Q) How to substitute bad with good word in 2nd
field position of file f1 ?
Ans - $ sed ‘s/bad/good/2’ f1<Enter – Key>
Q) How to substitute bad with good word in 1st field
position of file f1 ?
Ans - $ sed ‘s/bad/good/1’ f1<Enter – Key>
Q) How to substitute bad with good word from 3rd
to last field of file f1 between 15th
to 23rd lines ?
Ans - $ sed ’15,23s/bad/good/3g’ f1<Enter – Key>
Q) How to substitute bad with good word in 2nd
field position of file f1 at a line number 45 ?
Ans - $ sed ‘45s/bad/good/2’ f1<Enter – Key>
Q) How to replace the 5th occurance of 3rd
line of is with are in file f1 ?
Ans - $ sed ‘3s/is/are/5’ f1<Enter – Key>
Q) How to print blank lines ?
Ans - $ sed –n ‘^$p’ filename<Enter – Key>
No comments:
Post a Comment