在Perl中,我如何更改,删除,或在一个文件中插入一条线,或附加到文件的开始? [英] In Perl, how do I change, delete, or insert a line in a file, or append to the beginning of a file?

查看:329
本文介绍了在Perl中,我如何更改,删除,或在一个文件中插入一条线,或附加到文件的开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过修改,删除或插入线或附加到文件的开头更改文件的内容。我怎样才能做到这一点在Perl?

I want to make changes to the contents of a file by modifying, deleting or inserting lines or appending to the beginning of the file. How can I do that in Perl?

这是从的问题 =htt​​p://faq.perl.org/ >官方FAQ 。我们是<一个href=\"http://meta.stackexchange.com/questions/24244/can-we-import-the-perlfaq-into-stackoverflow\">importing在perlfaq堆栈溢出。

This is a question from the official FAQ. We're importing the perlfaq to Stack Overflow.

推荐答案

(这是官方perlfaq回答减去任何后续编辑)

(This is the official perlfaq answer, minus any subsequent edits)

从文本文件中插入,更改或删除的线的基本思想
包括读取和文件打印到时候你要打
变化,使得变化,则读取和打印文件的其余部分。
Perl并不为线(特别是因为记录提供随机访问
输入分隔符, $ / ,是可变的),虽然模块,如
领带::文件可以伪造的。

The basic idea of inserting, changing, or deleting a line from a text file involves reading and printing the file to the point you want to make the change, making the change, then reading and printing the rest of the file. Perl doesn't provide random access to lines (especially since the record input separator, $/, is mutable), although modules such as Tie::File can fake it.

一个Perl程序来完成这些任务需要打开一个文件的基本形式,
它的印刷线,然后关闭文件:

A Perl program to do these tasks takes the basic form of opening a file, printing its lines, then closing the file:

open my $in,  '<',  $file      or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";

while( <$in> )
    {
    print $out $_;
    }

close $out;

在这一基本形式,添加你需要插入,更改零件,或
删除线。

Within that basic form, add the parts that you need to insert, change, or delete lines.

要prePEND线开始,你进入之前打印这些行
循环打印现有生产线。

To prepend lines to the beginning, print those lines before you enter the loop that prints the existing lines.

open my $in,  '<',  $file      or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";

print $out "# Add this line to the top\n"; # <--- HERE'S THE MAGIC

while( <$in> )
    {
    print $out $_;
    }

close $out;

要改变现有的生产线,将code修改里面的线路
while循环。在这种情况下,code发现perl的所有小写版本
和uppercases他们。该发生的每一行,因此请确保你
应该做的每一行!

To change existing lines, insert the code to modify the lines inside the while loop. In this case, the code finds all lowercased versions of "perl" and uppercases them. The happens for every line, so be sure that you're supposed to do that on every line!

open my $in,  '<',  $file      or die "Can't read old file: $!";
open my $out, '>', "$file.new" or die "Can't write new file: $!";

print $out "# Add this line to the top\n";

while( <$in> )
    {
    s/\b(perl)\b/Perl/g;
    print $out $_;
    }

close $out;

要改变只有一个特定的行,输入行号,$,是非常有用的。
首先阅读和打印排队到要改变的人。其次,阅读
单线你想改变,改变它,并打印出来。之后,
读出的行的其余部分和打印这些

To change only a particular line, the input line number, $., is useful. First read and print the lines up to the one you want to change. Next, read the single line you want to change, change it, and print it. After that, read the rest of the lines and print those:

while( <$in> )   # print the lines before the change
    {
    print $out $_;
    last if $. == 4; # line number before change
    }

my $line = <$in>;
$line =~ s/\b(perl)\b/Perl/g;
print $out $line;

while( <$in> )   # print the rest of the lines
    {
    print $out $_;
    }

要跳过的行,使用循环控制。在这个例子中跳过下一
注释行,并且一旦遇到任最后停止所有处理
__ __ END __ __ DATA

To skip lines, use the looping controls. The next in this example skips comment lines, and the last stops all processing once it encounters either __END__ or __DATA__.

while( <$in> )
    {
    next if /^\s+#/;             # skip comment lines
    last if /^__(END|DATA)__$/;  # stop at end of code marker
    print $out $_;
    }

做同样的事情用一个以跳到删除特定行
行,你不希望在输出中出现。这个例子跳过每
第五行:

Do the same sort of thing to delete a particular line by using next to skip the lines you don't want to show up in the output. This example skips every fifth line:

while( <$in> )
    {
    next unless $. % 5;
    print $out $_;
    }

如果,一些奇怪的原因,你真的想看到整个文件一次
而不是加工生产线由行,你可以在(只要你能啜食其
适合在内存中的整个事情)!

If, for some odd reason, you really want to see the whole file at once rather than processing line-by-line, you can slurp it in (as long as you can fit the whole thing in memory!):

open my $in,  '<',  $file      or die "Can't read old file: $!"
open my $out, '>', "$file.new" or die "Can't write new file: $!";

my @lines = do { local $/; <$in> }; # slurp!

    # do your magic here

print $out @lines;

模块,如档案::嘟嘟地喝
领带::文件可以与帮助
太。如果你可以,但是,避免读取整个文件一次。 Perl将不会
给该存储器返回给操作系统,直到过程完成

Modules such as File::Slurp and Tie::File can help with that too. If you can, however, avoid reading the entire file at once. Perl won't give that memory back to the operating system until the process finishes.

您也可以使用Perl单行修改就地文件。下列
改变inFile.txt所有'弗雷德'到'巴尼',覆盖的文件
新的内容。随着 -p 开关,Perl的环绕在code while循环
-e 指定和 -i 打开就地编辑。目前
线是 $ _ 。随着 -p ,Perl的自动打印 $的价值_
在循环的结束。请参见 perlrun 了解更多详情。

You can also use Perl one-liners to modify a file in-place. The following changes all 'Fred' to 'Barney' in inFile.txt, overwriting the file with the new contents. With the -p switch, Perl wraps a while loop around the code you specify with -e, and -i turns on in-place editing. The current line is in $_. With -p, Perl automatically prints the value of $_ at the end of the loop. See perlrun for more details.

perl -pi -e 's/Fred/Barney/' inFile.txt

要做出inFile.txt的备份,给-i文件扩展名补充:

To make a backup of inFile.txt, give -i a file extension to add:

perl -pi.bak -e 's/Fred/Barney/' inFile.txt

要改变只有第五行,可以添加一个测试检查 $ ,输入
行号,则只有当测试通过执行操作:

To change only the fifth line, you can add a test checking $., the input line number, then only perform the operation when the test passes:

perl -pi -e 's/Fred/Barney/ if $. == 5' inFile.txt

要前一定行前加行,您可以添加行(或行!)
Perl的版画 $ _

To add lines before a certain line, you can add a line (or lines!) before Perl prints $_:

perl -pi -e 'print "Put before third line\n" if $. == 3' inFile.txt

可以甚至添加一行到文件的开始,因为在当前行
在循环结束打印:

You can even add a line to the beginning of a file, since the current line prints at the end of the loop:

perl -pi -e 'print "Put before first line\n" if $. == 1' inFile.txt

要插入一个已经在文件后一条线,使用 -n 开关。它的
就像 -p 除了它不打印 $ _ 在循环的结束,所以
你必须做你自己。在这种情况下,打印 $ _ 第一,然后打印
要添加一行。

To insert a line after one already in the file, use the -n switch. It's just like -p except that it doesn't print $_ at the end of the loop, so you have to do that yourself. In this case, print $_ first, then print the line that you want to add.

perl -ni -e 'print; print "Put after fifth line\n" if $. == 5' inFile.txt

要删除行,只打印你想要的。

To delete lines, only print the ones that you want.

perl -ni -e 'print unless /d/' inFile.txt

&hellip;或hellip;

… or …

perl -pi -e 'next unless /d/' inFile.txt

这篇关于在Perl中,我如何更改,删除,或在一个文件中插入一条线,或附加到文件的开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆