A file may contain different types of strings, numbers, special characters, etc..,. We can directly edit the file using a file editor, but that applies to a single file only. If we wish to search and replace a particular word or number on multiple files, it is a burden to edit manually on every single file. In such situations we use Perl and Sed commands to achieve it.
  
    In this article we are going to see how to replace strings, numbers in a file or a set of files using perl command and sed command.

Using Perl:

    Perl is the command interpreter of the Perl programming language. Perl has a large number of options that can be useful on the command line. In this article we will be seeing how to perform search and replace alone as stated earlier.

Syntax:

perl -pi -e 's/search/replace/g' File1 File2 File3


    The above syntax is the basic one, but it differs according to the type of search and replacement (Strings, Numbers, Special Characters).

Demo file:

    Our demo file will be,

jhony@ljunix:~$ cat demo.txt
Hi LJunix users iam a demo file containing some strings and numbers.
The string would be Good day all!!!.
The number would be 222 and 27.5 and my IP is 192.88.64.36
lastly one<=two

Replacing a string on files:

    To replace a string on the files, we can use the basic syntax.

$ perl -pi -e 's/search/replace/g' File_names

jhony@ljunix:~$ perl -pi -e 's/all/everyone/g' demo.txt
jhony@ljunix:~$ cat demo.txt
Hi LJunix users iam a demo file containing some strings and numbers.
The string would be Good day everyone!!!.
The number would be 222 and 27.5 and my IP is 192.88.64.36
lastly one<=two


Replacing Numbers on files:

    The basic syntax applies to the normal numbers but if the number is in float or an IP we need to add an escape character ( \ ) before the period.

$ perl -pi -e 's/27\.5/34\.7/g' File_names

    On the above command the perl search for 27.5 on the file and replace it with 34.7. Such scenarios happens when we want to change an IP address on files.

$ perl -pi -e 's/192\.88\.64\.36/192\.77\.100\.22/g' File_names

jhony@ljunix:~$ perl -pi -e 's/192\.88\.64\.36/192\.77\.100\.22/g' demo.txt
jhony@ljunix:~$ cat test.txt
Hi LJunix users iam a demo file containing some strings and numbers.
The string would be Good day everyone!!!.
The number would be 222 and 27.5 and my IP is 192.77.100.22
lastly one<=two

Replacing strings with specials characters on files:

    We use the same escape character ( \ ) before the special characters. This applies to words consist of -,<,>,(,) or any other symbols.

$ perl -pi -e 's/one\<=two/one\<two/g' File_name

jhony@ljunix:~$ perl -pi -e 's/one\<=two/one\<two/g' demo.txt
jhony@ljunix:~$ cat demo.txt
Hi LJunix users iam a demo file containing some strings and numbers.
The string would be Good day everyone!!!.
The number would be 222 and 27.5 and my IP is 192.77.100.22
lastly one<two


Using Sed:

    SED stands for stream editor. It can perform a lot of function on a file other than replacing such as searching, insertion or deletion in a file without opening it. We will be seeing how to perform search and replace through Sed.

Syntax:

sed -i -e 's/search/replace/g' File1 File2 File3


    We will be using the same demo file demo.txt.

Replacing a string on files:

    We can use the basic syntax for replacing a word across all files,

$ sed -i -e 's/search/replace/g' demo.txt

jhony@ljunix:~$ sed -i -e 's/all/everyone/g' demo.txt
jhony@ljunix:~$ cat demo.txt
Hi LJunix users iam a demo file containing some strings and numbers.
The string would be Good day everyone!!!.
The number would be 222 and 27.5 and my IP is 192.88.64.36
lastly one<=two

Replacing a Number on files:

    The advantage of sed is it can replace any kind of value with the same syntax,

$ sed -i -e 's/27.5/34.7/g' demo.txt

jhony@ljunix:~$ sed -i -e 's/192.88.64.36/192.77.100.22/g' demo.txt
jhony@ljunix:~$ cat demo.txt
Hi LJunix users iam a demo file containing some strings and numbers.
The string would be Good day everyone!!!.
The number would be 222 and 27.5 and my IP is 192.77.100.22
lastly one<=two

Replacing strings with specials characters on files:

    The basic syntax applies to the words that carries special characters even space between phrases,

$ sed -i -e 's/search/replace/g' demo.txt

jhony@ljunix:~$ sed -i -e 's/one<=two/one<two/g' demo.txt

jhony@ljunix:~$ sed -i -e 's/one<=two/one<two/g'' demo.txt
jhony@ljunix:~$ cat demo.txt
Hi LJunix users iam a demo file containing some strings and numbers.
The string would be Good day everyone!!!.
The number would be 222 and 27.5 and my IP is 192.77.100.22
lastly one<two

    Hence Sed has some advantages over Perl command, we will see some useful Sed commands in upcoming articles.

    Hope this article helps you, feel free to ask if you have any questions.

Comments

  1. Be the first to add a comment.