如何匹配以特定单词开头的行?

时间:2015-12-14 21:29:03

标签: perl

我有一个Perl脚本,当前抓取包含单词ACCOUNT的每一行。我怎样才能在行的开头抓取包含单词ACCOUNT的行?

use strict;

my $find = 'ACCOUNT';

open (NEW, ">", "output.txt" ) or die "could not open:$!";
open (FILE, "<", "Report.txt") or die "could not open:$!";

while (<FILE>) {
    print NEW if (/$find/);
}

close (FILE);
close (NEW);

1 个答案:

答案 0 :(得分:2)

评论了空白版,因为人们似乎没有阅读评论。

use strict;
my $find = 'ACCOUNT';

open (NEW, ">", "output.txt" ) or die "could not open:$!";
open (FILE, "<", "Report.txt") or die "could not open:$!";

while (<FILE>) {
    # print NEW if $_ =~ /^\s*$find/ ; # Any line that starts with whitespace and followed by $find
    print NEW if $_ =~ /^$find/    ; # Any line that starts strictly with $find
}
close (FILE);
close (NEW);