SHELL

SED Commands

Admin
November 16, 2020
5 min read

SED commands and their usages

When we need to search, replace, insert, delete in a file then we use SED command. Opening a file in editor and then do search and replace will be lengthy process, to avoid this manual effort we use SED.

1. Replacing with SED

To replace with the help of the SED below is the format.

$> sed ‘s/<first string>/<second string>/g’ <File_Name>

s -> Substitution
first string -> the string to be replaced
second string -> the string by which first string will get replaced
g -> substitute operator
File_Name -> The one we are looking for search and replace

If we will not put “g” at the end then the command will replace only first occurrence in a line, if there are more than one occurrence like second, third or so then it won’t replace it.

$> sed ‘s/<first string>/<second string>/’ <File_Name>

If one wants to replace only the nth occurrence, then use number instead of “g”.

$> sed ‘s/<first string>/<second string>/n’ <File_Name>

Eg:

$> sed ‘s/<first string>/<second string>/2’ ##Here only second occurrence will get replaced in lines present in the file.

If there is requirement of replacing all the occurrence after an occurrence, then we need to use below command. Where n can be 1,2,3,4 and so on.

$> sed ‘s/<first string>/<second string>/ng’ <File_Name>

Eg:

$> sed ‘s/<first string>/<second string>/2g’ <File_Name>

##This command will replace from 2nd occurrence to end of the lines.

If you want to replace the string at some particular line only rather than each lines, then use below command. “n” is the line number below.

$> sed ‘n s/<first string>/<second string>/g’ <File_Name>
If one wants to replace the string at in between some lines in the file, then use below command. N1 and n2 are the lines present in the file in between substitution is going to happen.

$> sed ‘n1,n2 s/<first string>/<second string>/g’ <File_Name>

Similar to above one if we want to replace the lines from a particular line to end then one can use below command. As we know $ represents the end so n1 and $ fulfill the requirement.

$> sed ‘n1,$ s/<first string>/<second string>/g’ <File_Name>

If one wants to duplicate the lines where the replacement has happened then replace “g” to “p” as per below command.

$> sed ‘s/<first string>/<second string>/p’ <File_Name>

If one wants to print the lines only the replaced one, then use below command.

$> sed -n ‘s/<first string>/<second string>/p’ <File_Name>

2. Deleting with SED

To delete the line in a file we again need not to open and do manual process, instead we can do it with the help of SED command without opening it.

  • To delete n number line, use below command.

sed ‘nd’ <File_Name>
n -> number of lines
d -> delete operation
File_Name -> File name in the operation will happen

Eg:

Sed ‘2d’ <File_Name>

  • To delete the last lines in a file.

sed ‘$d’ <File_Name>

  • To delete the range of lines. Where n1 and n2 represents a range.

sed ‘n1,n2d’ <File_Name>

  • To delete from a line to end of line.

sed ‘n,$d’ <File_Name>

  • To delete a pattern matching lines.

sed ‘/string pattern/d’ <File_Name>

Few interesting examples

sed 's/\s\+/\n/g' cell.tcl > 1 #How to make a list of objects in line (Breaking series of words and put it in column).

 

Share the Article