"Reprint" Perl modifies the original file contents

Source: Internet
Author: User



Many times, we want to make certain changes to the content of the original file, although Linux has sed this powerful text editing command, but he can only generate new files or print to screen (perhaps Sed also has the function of modifying the original file, small Caishuxueqian, not yet know, please tell me), But there's a command in Perl, $^i.

Example:


1 #!/usr/bin/perl
2 use strict;
3 $^I="";
4 while(<>){
5 s/^rate.*/rate=0/;
6 s/^testMode.*/testMode=0/;
7 print;
8 }


The value of $^i can be determined to overwrite the file, or to back up the file, to override, just set its value to empty, to back up, then $^i= ". Bak", he will name the changed file as 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 data for a particular file, if there is a function to automatically modify the file, it will be very useful. For example, when you need to move a string or command in a file, this feature can be leveraged when you want to change the IP address of many files.




Manually changing these files can take a long time, especially if it's a repetitive task. However, you can use the Perl program to do this. All Linux vendors have this program. Perform the following actions:
$ Perl-pi-e ' s| [old_string]| [New_string]|g ' [File]




The string can be a simple command or a regular expression. If you want to change the directory location (example: from/var/spool/mail to/var/spool/messages), you want to change each file in 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 example, we use search and xargs to make sure that the system runs only regular file commands, rather than commands that are not very well-regulated directories or other file types. This also runs the/etc directory tree, and changes all files recursively.




Because of us/ID, we must enter a path such as/var rather than/var. We can also use delimiters in Perl expressions. (in addition to using |, you can also use/, #,@ as a delimiter.) )
Take the following action to remove a command from the entire file
$ Perl-pi-e ' s|myword| | G ' File




This is basically not using anything but instead of "MyWord". (Note that there is no space between the second and third separators.)




Using Perl in this way, you can be very creative when you modify a configuration file or any other type of data. It is a function of automatic search and substitution.



"Reprint" Perl modifies the original file contents


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.