Code for reading and writing Perl text files, renaming and deleting files, and merging multiple text files

Source: Internet
Author: User
Tags glob

Read files:

Copy codeThe Code is as follows :#! Perl
Open filetxt, "/path/a.txt"; # filetxtis the file handle and is used to establish a link with file a.txt. The file handle can be named at will, but it should not be the same as several file handles that come with Perl.
Print <filetxt>; # This printexample is used to display the.txt file. <> Is the row fetch operator. <file handle> is used to read the content of the linked file.
Close filetxt; # close the file handle filetxt. Another way to disable it is to associate it with other files, such as open filetxt and "B .txt". If this is the case, the association with the original file a.txt is automatically disabled.

Or

Copy codeThe Code is as follows :#! Perl
Open filetxt, "<path/a.txt"; # <is used to read content from the file, but cannot write any content to the file. <It has the same effect as any symbol.
While ($ line = <filetxt>)
{
Print $ line;
)
Close filetxt;

Write File:

In the open line, <change to> or> to write content to the file. For example:

Copy codeThe Code is as follows :#! Perl
Open TXT, "> a.txt ";
Print TXT "I do not know if I am a customer in my dream, \ n"; # If the.txt file exists, its content will be replaced
Print TXT "is greedy. \ N "; # the content of this row will be appended to the second line
Close TXT;
[Code]

Or

#! Perl
Open TXT, "> a.txt ";
Print TXT "does not rely on the column alone, without limit, it is easy to see when you are not. \ N "; # If the.txt file already exists, the content of this row will be appended to the end of the existing file
Close TXT;

<, Read content from the file, but cannot output any content to the file. If no file is read, the effect of "<" is the same.
>, Output the content to the file, and clear the content in the original file.
>>, Append the content to the file, and the content in the original file will not be cleared.

File Rename and deletion:

Rename: rename the file.

Copy codeThe Code is as follows :#! Perl
Rename "a.txt", "B .txt"; # or rename "a.txt", "a. bat ";

Unlink: equivalent to rm, which deletes some files in the system.

Copy codeThe Code is as follows :#! Perl
My @ files = <*. txt>; # Or my @ files = glob "*. txt ";
Unlink @ files;

OrCopy codeThe Code is as follows :#! Perl
Unlink glob "*. txt ";

OrCopy codeThe Code is as follows :#! Perl
Unlink <*. txt>;

Merge multiple text files:

In Perl, You can merge multiple text files as follows:

Copy codeThe Code is as follows :#! Perl
Open a, "> a.txt ";
Print a "over 40 years, home country, three thousand miles. ";
Open B, "> B .txt ";
Print B "fengge longlou Lian Shaohan, Yushu qiongzhi as the smoke radish. ";
Open c, "> c.txt ";
Print c "How long have you known dago? "; # Create text files a.txt0000b.txtand c.txt respectively, and perform write operations
Close;
Close B;
Close c; # close the corresponding file handle (filehandle)
Open a, "> a.txt ";
Open B, "B .txt ";
Open c, "c.txt"; # re-establish file association, where a.txt performs additional write operations, and the other two files perform read Operations
Print a "\ n". <B>. "\ n"; # use. To connect strings, this operator must
Print a <c>. "\ n"; # <filehandle> indicates the content of the text file linked to the file handle.
Close;
Close B;
Close c;
[Code]
Or
[Code]
#! Perl
Open a, "> a.txt ";
Print a "over 40 years, home country, three thousand miles. \ N ";
Open B, "> B .txt ";
Print B "fengge longlou Lian Shaohan, Yushu qiongzhi as the smoke radish. \ N "; # When a file is generated and the content is written, a line break is executed to keep the cursor in the next line.
Open c, "> c.txt ";
Print c "How long have you known dago? \ N "; # create text files a.txt0000b.txtand c.txt respectively, and perform write operations
Close;
Close B;
Close c; # close the corresponding file handle (filehandle)
Open a, "> a.txt ";
Open B, "B .txt ";
Open c, "c.txt"; # re-establish file association, where a.txt performs additional write operations, and the other two files perform read Operations
Print a <B>; # use. To connect strings. This operator is required.
Print a <c >;#< filehandle> indicates the content of the text file linked to the file handle.
Close;
Close B;
Close c;

If you want to delete B .txt and c.txt after merging text files, add the following code:

Unlink <B .txt>;
Unlink <c.txt>;

Or

Unlink "B .txt ";
Unlink "c.txt ";

Or

Unlink <B .txt>, <c.txt>;

Or

Unlink "B .txt", "c.txt ";

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.