What I am putting down here is more for me rather than for anyone else. When you work with a file, it is often required to change the format of the data in the entire file or edit a certain pattern in there. And when the file size is large, this becomes a highly tedious task. And stream editors like sed are ideal for this purpose.
Well, this post only highlights the use of the substitution command or the s command for sed.
The syntax is as follows:
sed 's/pattern to be replaced/pattern to replace with/' filename
The "filename" here refers to the input file. And the output works just like the linux command 'cat'. It will be displayed in the terminal.
So, in order to write to a file, you can use
sed 's/pattern to be replaced/pattern to replace with/' filename > outfilename
There are also some extra additions to this command to obtain certain results:
i) sed in the above shown form only works on the first pattern found in each line. To operate on all recurring patterns in a line the g command is used.
sed 's/pattern to be replaced/pattern to replace with/g' filename > outfilename
ii) In order to delete a pattern you can just leave the second delimiter blank
sed 's/pattern to be replaced//' filename > outfilename
iii) In order to look for a group of characters say a-z and replace each with a particular pattern then
sed 's/[a-z]/pattern to replace with/' filename > outfilename
iv) if you need to replace every other character than say a-z with a particular pattern then
sed 's/[^a-z]/pattern to replace with/' filename > outfilename
v) In order to operate on a particular pattern(say a tab) and the rest of the line
sed 's/[\t].*/pattern to replace with/' filename > outfilename
I believe a combination of these are good enough to edit a basic text file. If you need a comprehensive tutorial it can be found here: