sed is a wonderful Linux utility with lot of options, in my opinion the only other Unix utility which can outdo it, is none other then awk , here are some examples which will help you to understand beauty of sed
Consider file Example.txt for this :
Now lets say if you want to view only second line of this file, sed comes to your rescue
Remember to use "-n" option or else following would happen , since default behavior of sed with "p" option is print all the line, "-n" will suppress that
Now what if you want to view all the lines except the third line, try as mentioned below
using "d" you "deleted" the unwanted line , remember you have not deleted anything in original "Example.txt" file , its just a view , to modify original file you will need to use -i option
there is one more useful option and it is "q" ( quit )
if you want to view only first line and want to quit after it, this is how you use it
if you want to print all the lines which starts with a certain character ( Lets say A ), following example is going to help you, "^" signifies start of a line
Printing a line with ending with certain character is not difficult either,
i have printed line ending with "5" , here "$" signifies end of a line
sed is also a very useful for substitution ( with s option ), consider following file "Subsitute.txt" as an example, it has three columns separated by comma ( , ) and four rows
Now following example can help you if you want to substitute all the occurrence of "xyz" with "XYZ"
but what if you want to keep "xyz" untouched in first row and change the all other occurrence of "xyx" in from 2nd row till end ?
it was quite easy right ? ( See that "$" again )
Now you need to replace "xyz" only in second row , its soooooo simple !!!!!!
Alright So far You must have noticed "g" and many of you have already guessed it right, its for "global substitution " , if you use g option it substitutes all occurrence of intended pattern in ALL columns
however in real world you may not want this, there may be situations when you want to replace only first occurrence of a pattern in a particular row
Just replace the g with occurrence number you want to replace and it will do the trick for you
Observe above result carefully , the Second occurrence of "xyz" in fourth row was not replaced , why ?
Since we only meant to replace the first occurrence of "xyz" in our command
sed is really very feature rich, its not easy to cover all of them in single post
so please do wait for my future posts
please comment and share if you liked my post, your suggestions are always welcomed and will help me in improving my future posts
Visit Other Posts -
if you want learn to Basic Unix Commands in 1 Hour, here is the link
Basic Unix Commands in 1 Hour
if you want learn Unix/Linux Commands in detail, here is the link
Learn Unix/Linux Commands in detail
Also keep visiting my blog to learn more
unixtechworld.blogspot.com
Consider file Example.txt for this :
Now lets say if you want to view only second line of this file, sed comes to your rescue
Remember to use "-n" option or else following would happen , since default behavior of sed with "p" option is print all the line, "-n" will suppress that
Now what if you want to view all the lines except the third line, try as mentioned below
using "d" you "deleted" the unwanted line , remember you have not deleted anything in original "Example.txt" file , its just a view , to modify original file you will need to use -i option
there is one more useful option and it is "q" ( quit )
if you want to view only first line and want to quit after it, this is how you use it
if you want to print all the lines which starts with a certain character ( Lets say A ), following example is going to help you, "^" signifies start of a line
Printing a line with ending with certain character is not difficult either,
i have printed line ending with "5" , here "$" signifies end of a line
sed is also a very useful for substitution ( with s option ), consider following file "Subsitute.txt" as an example, it has three columns separated by comma ( , ) and four rows
Now following example can help you if you want to substitute all the occurrence of "xyz" with "XYZ"
but what if you want to keep "xyz" untouched in first row and change the all other occurrence of "xyx" in from 2nd row till end ?
it was quite easy right ? ( See that "$" again )
Now you need to replace "xyz" only in second row , its soooooo simple !!!!!!
Alright So far You must have noticed "g" and many of you have already guessed it right, its for "global substitution " , if you use g option it substitutes all occurrence of intended pattern in ALL columns
however in real world you may not want this, there may be situations when you want to replace only first occurrence of a pattern in a particular row
Just replace the g with occurrence number you want to replace and it will do the trick for you
Observe above result carefully , the Second occurrence of "xyz" in fourth row was not replaced , why ?
Since we only meant to replace the first occurrence of "xyz" in our command
sed is really very feature rich, its not easy to cover all of them in single post
so please do wait for my future posts
please comment and share if you liked my post, your suggestions are always welcomed and will help me in improving my future posts
Visit Other Posts -
if you want learn to Basic Unix Commands in 1 Hour, here is the link
Basic Unix Commands in 1 Hour
if you want learn Unix/Linux Commands in detail, here is the link
Learn Unix/Linux Commands in detail
Also keep visiting my blog to learn more
unixtechworld.blogspot.com