For 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
The noob Commands
1) Substitute Command
......................................................................................
a) single substitution
Syntax
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.
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,
Output:
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
here we go
c) Multiple substitution
Syntax
or
e.g.
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;
..Replace "the" with "eht"
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.
....................................................................................
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
where things inside square brackets [] are optional.sed [option] 'command' [filename]
The noob Commands
1) Substitute Command
......................................................................................
a) single substitution
Syntax
wheresed [option] 's/{old_value}/{new_value}' [filename]
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.
Output:echo professionals built titanic while amateur built ark | sed 's/amateur/lone amateur/'
Explainanton:professionals built titanic while lone amateur built ark
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,
guess what only first occurence will be replace and output will beecho professionals built titanic while amateur built ark | sed 's/built/made/'
Output:
Actually sed work by reading line and processing the frst occurence in each line leaving all other untouched.professionals made titanic while amateur built ark
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
e.g.sed [option] 's/{old_value}/{new_value}/g' [filename]
Output:echo professionals built titanic while amateur built ark | sed 's/built/made/g'
Now its working fine. What about multiple word substituion ?professionals made titanic while amateur made ark
here we go
c) Multiple substitution
Syntax
sed [option] 's/{old_value1}/{new_value1}/ ; s/{old_value2}/{new_value2}/' [filename]
or
-e means extension, it is an option to tell sed that there are more than one substituion.sed [option] -e 's/{old_value1}/{new_value1}/' -e 's/{old_value2}/{new_value2}/' [filename]
e.g.
Output:echo professionals built titanic while amateur built ark | sed 's/professionals/experts/ ; s/amateur/noobs/'
orexperts built titanic while noobs built ark
echo professionals built titanic while amateur built ark | sed -e 's/professionals/experts/' -e 's/amateur/noobs/'
How about fetching a real file to do some job, uptil now i was using a string.experts built titanic while noobs built ark
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;
Now we shall try out some substitution with further additon to ur knowledge.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.
..Replace "the" with "eht"
Output:sed 's/the/eht/' s
lol..the thing didnt work out as expected it replaced "the" in "then" at last line changin it to "ehtn"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.
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.
Output:sed 's/\<the\>/eht/' s
Thats all for now, will keep it updatingOnce 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.
....................................................................................