简体   繁体   中英

I'm trying to grep a pattern in multiple files and print the corresponding matching lines to different new files.

I tried it for 1 file but the output it's giving is : HASH(0x61fff0). Please suggest where i'm going wrong.

use File::Grep qw( fgrep );
use strict;
use warnings;
my $file="outfile1.txt";
open(FL,">","$file");
my @matches = fgrep { /"first"/ } glob "./file1.txt";
print FL $_ foreach @matches;

grep is only used to match a particular keyword, to split and then print the matching pattern..

use strict;
use warnings;
my $file="outfile1.txt";
open(FL,"$file");
my @f=<FL>;
my @matches = grep {m/first/g} @f;
chomp,print "$_\n", foreach(@matches);

Use File::Grep::fmap instead of File::Grep::fgrep

use File::Grep qw( fgrep fmap );
...
my @matches = fmap { /"first"/ ? ($_) : () } glob "./file1.txt";

OR use more complicated code for printing File::Grep::fgrep results

foreach my $match (@matches){
  my $filename =  $match->{filename};
  my $count = $match->{count};
  foreach $pos (sort{$a<=>$b} keys %{$match->{matches}}) {
     print FL $match->{$pos};
  }
}
use strict;
use warnings;

my $file= "outfile1.txt";
open (FL,'>$file');

my @glob_matches =  glob ("./file1.txt");

my @grep_matches = grep(/first/, @glob_matches);

foreach my $filtered_files (@grep_matches){

print FL $filtered_files."\n";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM