sed
sed
(stream editor) is a non interactive text editor which is considerably faster than interactive text editors. The command line syntax for sed is:sed
[ options …][command][files …]The different permissible options are:
-f
file the file contains editing commands-n
suppress automatic printing of lines-e
the script is made up of one or more commands
Following are some sed examples
1.
Print all lines from the file phonebook that end with 76 :sed -n '/
76$/p' phonebookNotice /76$/ tells sed to search for lines which end with 76. Dollar sign means end of the line. The -n is needed and p means print.
2.
Print all lines from the file phonebook that start with Su :sed -n '/^Su/p' phonebook
Notice /^Su/ tells sed to search for lines that start with SU. Carret ^ sign means beginning of the line.
3.
Print all lines from the file phonebook that contain 76 :sed -n '/76/p' phonebook
Notice /76/ tells sed to search for lines which contains 76. This has the
same effects as the Unix grep, fgrep and egrep commands.
4.
Print all lines from the file phonebook that showing nonprinting characters (such as tab)sed -n '
l' phonebookNotice between the two single quotes is the
letter l not number 1.5.
Delete 4 lines from the file phonebook:sed '4d' phonebook
Notice d is for delete.
6.
Delete all lines from the file phonebook that start with Su :sed '
/^Su/d' phonebook
7.
Delete all lines that end with 76 from the file phonebook:sed '
/76$/d' phonebook
8.
Delete all lines that contain 76 from the file phonebook::sed
'/76/d' phonebook
10.
Delete all lines that contain Billy or billy from the file phonebook:sed '
/[Bb]illy/d' phonebookNotice the rgular expression [Bb] first takes B (hence it will be Billy) and next takes b (that makes billy).
11.
Replace the first 4 characters of each line of the file phonebook by MOONsed '
s/..../MOON/' phonebookNotice s stands for substitute, and period . is the regular
expression which means any character. Thus the expression
s/..../MOON/ means search for the first 4 characters and substitute with MOON.
12.
Replace the last 4 characters of each line of the file phonebook by MOONsed '
s/....$/MOON/' phonebook
13.
Delete the first 4 characters of each line of phonebooksed '
s/....//' phonebookNotice the subsitute pattern
// is null, means replace it by nothing.14.
Delete the last 4 characters of each line of the file phonebooksed
's/....$//' phonebook
15.
Replace the first r found in all lines that contain 98 with R ofthe file phonebook.
sed '
/98/s/r/R/' phonebookNotice only Barbara Swingle has 98 and r. Thus the name will be: Ba
Rbara Swingle.
16.
Replace all r found in any lines that contains 98 with R of the file phonebook.sed '/
98/s/r/R/g' phonebookNotice the g means global. Only Barbara Swingle has 98 and r. Thus the name will be:
Ba
RbaRa Swingle.
17.
Print all lines between the line that starts with BEGIN and another line, which starts with END from file called awk5.sed
-n '/^BEGIN/,/^END/p' awk5Notice the
, between /BEGIN/ and /END/