How to use sed command to replace string in Unix

The sed (stream editor) editor reads text from the standard input, edits it according to a set of given commands (in a script or on the command line), and writes the result back to the standard output.

In fact, set of given commands can be very complex and you can do very clever things with ‘sed’, but nowadays it is better to use modern scripting language like Python or Perl according your requirement.

If the editing script or commands are simple enough, it can be included on the command line, but a more complicated script can be stored in a file.

The most common use of a sed is to replace all occurrences of a particular string with another string.

Syntax:

$ sed 's/old_string/new_string/g' input_file.txt > output_file.txt

For example, the following command replaces every occurrence of the string bull with the string steer and saves the edited version to output_file:

$ sed 's/ten/10/g' input_file.txt > output_file.txt

The ‘s’ function being used with the ‘g’ option works on the principle of search and replace on all occurrences of a string in the file.

We will test this with one example file, my file looks like:

[wot@unix ~] $ cat > input_file.txt
Hello
Unix commands has total ten floors
sed command is the tenant for them
awk is a tenant
perl often visits sed command
the end
^C
[wot@unix ~] $ ls
input_file.txt
[wot@unix ~] $ cat input_file.txt
Hello
Unix commands has total ten floors
sed command is the tenant for them
awk is a tenant
perl often visits sed command
the end
[wot@unix ~] $

After creating the sample file, I will apply the above command to this file and see the result.

[wot@unix ~] $ sed ‘s/ten/10/g’ input_file.txt > output_file.txt
[wot@unix ~] $ cat output_file.txt
Hello
Unix commands has total 10 floors
sed command is the 10ant for them
awk is a 10ant
perl of10 visits sed command
the end
[wot@unix ~] $

A search is made for a string, and whenever a match is found the string is replaced with another string.

The surrounding characters are ignored, so the above example not only changes ten to 10, it also changes tenants to 10ants.

Luckily, here search string is a regular expression, so you can prevent the conversion of tenants by skipping instances of ‘ten’ that are followed by an ‘a’ with the following command:

$ sed 's/ten[^a]/10/g’  input_file.txt > output_file.txt
[wot@unix ~] $ cat output_file.txt
Hello
Unix commands has total 10floors
sed command is the tenant for them
awk is a tenant
perl of10 visits sed command
the end
[wot@unix ~] $

This type of operation is very useful, but there is a bit more to sed than simple text replacement.


Note:
In this example, we’re using the slash as a separator. If you ever need to specify the separator character in the regular expression, put a backslash before it.

Above sed command can’t edit the input file directly, but if you want to edit the file directly, you have to use ‘-i’ option.

Comments

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: