When you write a script or install a program, you need to modify the specific file data, if there is the ability to automatically modify the file, it will be very practical. For example, when you need to move a string or command in a file, this feature can be used when you want to change the IP address of many files.
Manually changing these files can take a long time, especially if it is a repetitive task. However, you can use a Perl program to do this. This program is available to all Linux vendors. Perform the following actions:
$ Perl-pi-e ' s| [old_string]| [New_string]|g ' [File]
A string can be a simple command or a regular expression. If you want to change the location of the directory (for example: from/var/spool/mail to/var/spool/messages), you want to change each file under the directory/etc to reflect the path change. Perform the following actions:
$ find/etc-type f| Xargs perl-pi-e ' S|\/var\/spool\/mail|\/var\/spool\/messages|g '
In this case, we use search and xargs to ensure that the system runs only the commands of the regular files, not the commands for the very directory or other file types. This also runs the/etc directory tree, and recursively changes all files.
Because of us/logos, we must enter a path such as \/var instead of/var. We can also use delimiters in Perl expressions. (in addition to using |, you can use/, #,@ as a separator.) )
Remove a command from the entire file by doing the following
$ Perl-pi-e ' s|myword| | G ' File
This basically does not use any thing to replace "MyWord". (Note that there are no spaces between the second and third delimiters).
Using Perl in this way, when you modify a configuration file or any other type of data, you are extremely creative. It is a function of automatic search and substitution.