SED - Ultimate tool for linux users
December 7, 2007 – 2:18 amFor last few days am learning this powerful utility in linux, though i confess that i did’t pay much attention to it when it was in my syllabus.
But now I realised how powerful it can be to use. Though I havent perfectly masterd this utility but still keep on updating the thread as i find out more.
—————————————————————————
Everything is considered as stream in Linux. SED utilises the same concept to exploit it to make things easier for system administrators. I recommend to hav some basic knowledge of Regular Expressions before jumping in.
Syntax for this Utility
| sed [option] ‘command’ [filename] |
where things inside square brackets [] are optional.
The noob Commands
1) Substitute Command
………………………………………….. ………………………………
a) single substitution
Syntax
| sed [option] ’s/{old_value}/{new_value}’ [filename] |
where
s stands for substitute
old_value and new_value are self understood
/// these three forward slashes are required to enclose old_value and new_value, u can also use _ _ _ or : : : as long as the words dont contain them.
e.g.
Quote:
| echo professionals built titanic while amateur built ark | sed ’s/amateur/lone amateur/’ |
Output:
| professionals built titanic while lone amateur built ark |
Explainanton:
echo string : just echoes the string entered
“|” its a pipe that connects previous command output to the input of next command, here the “string” is supplied as input to “sed” command.
Now try the same command to replace built by made using the above syntax,
| echo professionals built titanic while amateur built ark | sed ’s/built/made/’ |
guess what only first occurence will be replace and output will be
Output:
| professionals made titanic while amateur built ark |
Actually sed work by reading line and processing the frst occurence in each line leaving all other untouched.
This can be overcome easily. Read next point.
b) Global substitution
For all occurences to be updated u will hav to use “g” switch.
Syntax
| sed [option] ’s/{old_value}/{new_value}/g’ [filename] |
e.g.
| echo professionals built titanic while amateur built ark | sed ’s/built/made/g’ |
Output:
| professionals made titanic while amateur made ark |
Now its working fine
. What about multiple word substituion ?
here we go
c) Multiple substitution
Syntax
| sed [option] ’s/{old_value1}/{new_value1}/ ; s/{old_value2}/{new_value2}/‘ [filename] |
or
| sed [option] -e ’s/{old_value1}/{new_value1}/’ -e ’s/{old_value2}/{new_value2}/’ [filename] |
-e means extension, it is an option to tell sed that there are more than one substituion.
e.g.
| echo professionals built titanic while amateur built ark | sed ’s/professionals/experts/ ; s/amateur/noobs/’ |
Output:
| experts built titanic while noobs built ark |
or
| echo professionals built titanic while amateur built ark | sed -e ’s/professionals/experts/’ -e ’s/amateur/noobs/’ |
| experts built titanic while noobs built ark |
How about fetching a real file to do some job, uptil now i was using a string.
d) Fetching a real file.
Make a new file in home directory (or anywhere else but be sure to cd that directory in terminal).
Enter some lines
Like i created a file named “s” whose contents are;
| Once upon a time there was a king he loved his people but then the day arrived the war broke and he was defeated all he wanted was,the freedom of his people but then whom to blame. |
Now we shall try out some substitution with further additon to ur knowledge.
..Replace “the” with “eht”
| sed ’s/the/eht/’ s |
Output:
| Once upon a time ehtre was a king he loved his people but ehtn the day arrived eht war broke and he was defeated all he wanted was,eht freedom of his people but ehtn whom to blame. |
lol..the thing didnt work out as expected it replaced “the” in “then” at last line changin it to “ehtn”
So what should we do ? Here comes regular expression to our help. Just remember that to replace a word take these word delimiter “\<” and “\>”, anything written between them is taken to be a word.
For finding “the” we will have to use “\<the\>”, as given below
e.g.
| sed ’s/\<the\>/eht/’ s |
Output:
| Once upon a time there was a king he loved his people but then eht day arrived eht war broke and he was defeated all he wanted was,eht freedom of his people but then whom to blame. |
Thats all for now, will keep it updating![]()













2 Responses to “SED - Ultimate tool for linux users”
very interesting, but I don’t agree with you
Idetrorce
By Idetrorce on Sat 15th Dec, 2007