Perl modifies original file contents

Source: Internet
Author: User

Http://www.cnblogs.com/morebetter/archive/2007/05/10/741752.html

Many times, we want to make some changes to the contents of the original file, although Linux has sed this powerful text editing command, but he can only generate new files or print on screen (perhaps Sed also has the ability to modify the original file, small talents, do not know, please tell me), But there's a command in Perl, $^i.
Example:
#!/usr/bin/perl
Use strict;

$^i= "";
while (<>) {
s/^rate.*/rate=0/;
s/^testmode.*/testmode=0/;
Print
}

The value of $^i can decide whether to rewrite the file, or to back it up, to rewrite it, just set its value to null, and to back it up, $^i= ". Bak" and he'll name the changed file Filename.bak.

Http://hi.baidu.com/uncletuuthu/blog/item/862adb0062248e04728da501.html

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.