The substitution option is one of the most useful options of a sed
command.
In particular, it allows to search a text file for lines that contain particular string (SEARCH_STRING
), word or match some regular expression and replace all occurrences of such lines with some REPLACEMENT_LINE
.
To replace the whole line using sed
, it is needed to add wildcards (.*
) before and after a SEARCH_STRING
.
SED – Find and Replace a String in a File
Use the following sed
command to search each line of a FILE
for a SEARCH_STRING
and replace each matched line with REPLACEMENT_LINE
:
$ sed -i 's/.*SEARCH_STRING.*/REPLACEMENT_LINE/' FILE
The options passed to the sed
command:
Option | Description |
---|---|
-i |
Edit FILE in place |
s/ |
Substitute each line that contains SEARCH_STRING with REPLACEMENT_LINE |
Be careful if you replace URLs with “/” character.
An example of how to do it:
Extracted from: http://www.sysadmit.com/2015/07/linux-reemplazar-texto-en-archivos-con-sed.htm
Для подставки URL в sed мне помогло
#!/bin/bash
path=”what_replace = http://example.com”
sed -i “s#.*what_replace.*#$path#g” filename
thanks a lot