删除包含Perl中某些单词的文本文件中的行

时间:2013-09-12 16:27:52

标签: perl

我想删除包含单词“script”的文本文件中的每一行,然后将剩下的内容写入另一个文件中。但保留originl文件。

像这样:

打开文件 删除任何带有“script”一词的行 然后输出删除后留下的任何内容。

2 个答案:

答案 0 :(得分:7)

perl -ne '/script/ or print' file > newfile

答案 1 :(得分:1)

grep -v script original.file > new.file

或者如果你真的需要perl:

#!/usr/bin/perl

use strict;
use warnings;

open(my $in, '<', 'input.txt')
    or die "Cannot open input.txt: $!";

open(my $out, '>', 'output.txt')
    or die "Cannot open output.txt: $!";

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

close($in);
close($out);

最后,如果你只是想匹配“脚本”,如果它是一个单词(而不是像“处方”或“脚本”那样更大的字符串的一部分),那么改变:

/script/

要:

/\bscript\b/
相关问题