Perl text file read and write operations, file renaming and deletion, multiple text file merging implementation code _perl

Source: Internet
Author: User
Tags glob

Read the file:

Copy Code code as follows:

#!perl
Open Filetxt, "/path/a.txt"; # filetxt is a file handle used to establish a link to the file a.txt. The file handle can be named arbitrarily, but not the same name as a few file handles from Perl.
Print <filetxt>; # Here the print function is used to display the contents of the file a.txt. <>,< The file handle > is used to read the contents of the linked file for the row operator.
Close Filetxt; # Close file handle filetxt. Another way to close it is to associate it with other files, such as Open Filetxt, "B.txt", and then automatically close the association with the original file a.txt.

Or

Copy Code code as follows:

#!perl
Open Filetxt, "<path/a.txt"; # < is used to read content from a file, but cannot write anything to a file. < does not add the same effect as any symbol
while ($line =<filetxt>)
{
Print $line;
)
Close Filetxt;

Write file:

You can write content to a file by changing < to > or >> in the open row. Such as:

Copy Code code as follows:

#!perl
Open TXT, ">a.txt";
Print TXT "Dream does not know the body is a guest, \ n"; # If the file a.txt exists, its contents will be replaced
Print TXT "a meal of greedy huan."                \ n "; # The contents of the line are appended to the second row
Close TXT;
[Code]

Or


#!perl
Open TXT, ">>a.txt";
Print TXT "Alone mo railing, infinite Jiangshan, not easy to see when difficult."     \ n "; # If the file a.txt already exists, the row content is appended to the back of the existing file
Close TXT;

Read the content from the file, but not the output to any content in the file. If nothing is added to the condition of reading the file in conjunction with the "<" effect the same.
Output the content to a file and empty the contents of the original file.
>>, appends content to a file and does not empty the contents of the original file.

Renaming and deletion of files:

Rename: Renaming a file

Copy Code code as follows:

#!perl
Rename "A.txt", "B.txt"; # or rename ' A.txt ', ' a.bat ';

Unlink: Equivalent to RM, removing some files from the system

Copy Code code as follows:

#!perl
My @files =<*.txt>; # or my @files =glob ' *.txt ';
Unlink @files;

Or
Copy Code code as follows:

#!perl
Unlink Glob "*.txt";

Or
Copy Code code as follows:

#!perl
Unlink <*.txt>;

Merging of multiple text files:

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

Copy Code code as follows:

#!perl
Open a, ">a.txt";
Print a "40 years of home country, 3,000 of mountains and rivers." ";
Open B, ">b.txt";
Print B "Feng ge long Loulian han, Yushu Jong sticks as smoke luo." ";
Open C, ">c.txt";
Print C "Guizen knowledge Wars?"              "; # Create text files A.txt, b.txt, and C.txt, and perform write operations
Close A;
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 writes and two other files perform read-file operations
Print a "\ n". <b>.                   \ n "; Use. For a string connection, this operator must
Print a <c>.                     \ n "; # <filehandle> represents the contents of a text file that is linked to reading a file handle
Close A;
Close B;
Close C;
[Code]
Or
[Code]
#!perl
Open a, ">a.txt";
Print a "40 years of home country, 3,000 of mountains and rivers." \ n ";
Open B, ">b.txt";
Print B "Feng ge long Loulian han, Yushu Jong sticks as smoke luo."       \ n "; # When the file is generated and the content is written, a wrapping operation is performed, leaving the cursor on the next line
Open C, ">c.txt";
Print C "Guizen knowledge Wars?"                               \ n "; # Create text files A.txt, b.txt, and C.txt, and perform write operations
Close A;
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 writes and two other files perform read-file operations
Print a <b>; Use. For a string connection, this operator must
Print a <c>; # <filehandle> represents the contents of a text file that is linked to reading a file handle
Close A;
Close B;
Close C;

If you want to delete text files B.txt and c.txt after merging text files, you can 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.