2017-08-10 25 views
0

我在帶反引號的perl腳本中使用grep。在perl中使用grep與正則表達式匹配

grep -r --include=*.txt -e '[a-zA-Z0-9]*\.[a-zA-Z]*$' $dir -n >> test.txt; 

我想過濾出以文件名結尾的行。

例子: FILE1.TXT包含:

This is a file about file.txt 
This file is about algorithms. 
File.txtbis contains several functions. 
There are also several files. 
One of the files is sample.c 
Another example is test.doc 

我希望我的grep返回以下行:

This is a file about file.txt 
One of the files is sample.c 
Another example is test.doc 

但我的grep命令不返回任何東西。

如果我刪除了「$」符號,則grep命令將返回文件的所有行,即使它與正則表達式不匹配。另外,我寧願過濾掉1個或多個字符,而不是0或更多,但grep只有*。我可以在grep中使用「+」作爲一個或多個字符嗎?

在反引號中使用grep有沒有限制?

+0

'過濾掉以文件名結尾的行 - - 你是什麼意思?延期?任何擴展? – fugu

+0

我已更新描述。我指的是任何擴展名。 –

+0

可以將'-e'修改爲'-P'或'-E'。 – CWLiu

回答

0
#!/usr/bin/env perl 

use File::Find qw(find); 

# use File::Slurper qw(read_lines); 

sub read_lines { 
    my $file_name = shift; 
    open my $fh, "<", $file_name or die "Couldn't open $file_name: $!"; 
    my @buf = <$fh>; 
    close $fh; 
    chomp @buf; 
    return @buf; 
} 

find(
    { 
     no_chdir => 1, 
     wanted => sub { 
      my $file_path = $File::Find::name; 
      next unless -f $file_path; 
      my $file_name = $file_path; 
      $file_name =~ s/^.*\///; # drop everything until last '/' 

      for (read_lines($file_path)) { 
       print "$_\n" if m/\Q$file_name\E\s*$/; 
      } 
     }, 
    }, 
    '.' 
); 

如果您需要參考其他文件,只是積累的文件名查找之前是這樣的:

#!/usr/bin/env perl 

use File::Find qw(find); 

# use File::Slurper qw(read_lines); 

sub read_lines { 
    my $file_name = shift; 
    open my $fh, "<", $file_name or die "Couldn't open $file_name: $!"; 
    my @buf = <$fh>; 
    close $fh; 
    chomp @buf; 
    return @buf; 
} 

my @file_paths =(); 

find(
    { 
     no_chdir => 1, 
     wanted => sub { 
      my $file_path = $File::Find::name; 
      next unless -f $file_path; 
      push @file_paths, $file_path; 
     }, 
    }, 
    '.' 
); 

my @file_names = map { 
    my $file_name = $_; 
    $file_name =~ s/^.*\///; # drop everything until last '/' 
    $file_name; 
} @file_paths; 

my $regexp = '(?:' . join('|', map { "\Q$_\E" } @file_names) . ')$'; 
$regexp = qr/$regexp/; 

for my $file_path (@file_paths) { 
    for (read_lines($file_path)) { 
     print "$_\n" if /$regexp/; 
    } 
} 
+0

@ chris-yo,如果這有幫助,請按下回答旁邊的複選標記將其標記爲已接受。謝謝! –

0

的grep -Er --include = * TXT -e「[A-ZA- Z0-9] +。[a-zA-Z] + $'$ dir -n >> test.txt;