How to use Sed command in Linux

SED command in Linux and some examples

Sed is a “Stream Editor” which is used to edit files and modify files directly without opening files.
It also helps us to filter content from files. There are many more use of this command. Below are the few examples showing usage of sed command.

Syntax :-

sed [OPTIONS] [EXPRESSION/SCRIPT] [INPUT_FILE]

In above syntax if we do not provide the Input_file as argument then it will automatically take the standard input.

OPTION USED IN SED COMMAND :-

-n, --quiet, --silent                    This option suppress automatic printing of pattern space.
-e script, --expression=script           Add expression or script to be executed.
-f script-file, --file=script-file       Add the contents of script-file to the commands to be executed.
-i[SUFFIX], --in-place[=SUFFIX]          Edit files in place (this makes a backup with file extension SUFFIX, if SUFFIX is supplied).
-l N, --line-length=N                    Specify the desired line-wrap length, N, for the "l" command.
--POSIX                                  Disable all GNU extensions.
-r, --regexp-extended                    Use extended regular expressions in the script.
-s, --separate                           Consider files as separate rather than as a single continuous long stream.
-u, --unbuffered                         Load minimal amounts of data from the input files and flush the output buffers more often.
--help                                   Display a help message, and exit.
--version                                Output version information, and exit.

 

SED Command :-

Sed command always reads one line from the input and removes any trailing line.
And then put it in the pattern space where it checks the condition which consists of some specific code needs to be checked then
it executes all the commands once the condition get matched.

Examples:-

cat test_file.txt

India is great country. India has a great PM.
Mr. Narendra Modi is The Great Prime Minister.
IndiaBharat Everyone should support him.

Now we will play with the text written in the above file :-

1) Replace a string from content

sed 's/India/Bharat' test_file.txt

Output:-

Bharat is great country. India has a great PM. 
Mr. Narendra Modi is The Great Prime Minister.
IndiaBharat Everyone should support him.

## Here the "s" specifies the substitution operation. 
The "/" are delimiters. The "India" is the search pattern and the "Bharat" is the replacement string.
And Sed command will only replace the first occurance of the string not for second, third or further till Nth occurance.

2) Replace Nth or Global occurance of the string in a file

sed 's/India/Bharat/2' test_file.txt (#This will replace the 2nd occurance of this string "India". Same as this you can mention any line number)

Output:-

India is great country. Bharat has a great PM. 
Mr. Narendra Modi is The Great Prime Minister.
IndiaBharat Everyone should support him.


Global Replacement :-

sed 's/India/Bharat/g' test_file.txt

Output:-

Bharat is great country. Bharat has a great PM. 
Mr. Narendra Modi is The Great Prime Minister.
BharatBharat Everyone should support him.

And if you want to start from any specific occurance of the string then you can use below mentioned syntax with "g" global option

sed 's/India/Bharat/3g' test_file.txt (## It will replace all the strings starts from 3rd occurance and will not change the first and second occurance of the string)

3) Using & character

Whenever we want to add some special characters to some string then we use "&" in sed command.

sed 's/India/{&}/' test_file.txt

Output:-

{India} is great country. Bharat has a great PM. 
Mr. Narendra Modi is The Great Prime Minister.
{India}Bharat Everyone should support him.

4) Change web url to another url

sed 's/http:\/\//www/' test_file.txt

In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work.

Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example.

sed 's_http://_www_' file.txt
sed 's|http://|www|' file.txt

5) Another example is switching the first three characters in a line

sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' test_file.txt

Output:-

ndia is great country. Bharat has a great PM. 
r. Narendra Modi is The Great Prime Minister.
ndiaBharat Everyone should support him.

6) Running multiple sed commands

sed 's/India/Bharat/' test_file.txt| sed 's/Prime/Indian/'

OR

sed -e 's/India/Bharat/' -e 's/Prime/Indian/' test_file.txt

7) Deleting lines using sed

sed '2 d' test_file.txt (Will delete the second line- particular line number)
sed '5,$ d' test_file.txt (Will delete the lines from 5th to end- range of lines)

8) Sed command to search a string

sed -n '/India/ p' test_file.txt - It will search for the string "India"

9) Ignore specific content while searching

sed -n '/India/ !p' test_file.txt - It will search the full content except the string "India".

10) Replace on the line where the particular string matches

sed '/India/ s/PM/People/' file.txt - It will search for "India" and in the matching line it will replace the string "PM" to "People"

That's it.

 

 

2 thoughts on “How to use Sed command in Linux

  • August 29, 2016 at 3:23 pm
    Permalink

    Concise tutorial .. Very Nice ..

    Reply
    • August 30, 2016 at 3:13 pm
      Permalink

      Thanks Rakesh 🙂

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *