Monday, 6 August 2018

Linux: sed - Command



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.

0 comments:

Post a Comment