perl - Shell command on each line of file -
i trying write perl script following:
1. open file which, in each line, contains location of multiple files (say file a, file b)
2. egrep on each of these files egrep "module|input|output"
3. store output of egrep same file name a, b etc in different directory)
there more constraints on egrep, can add them later.
my question how do steps 2 , 3? in, execute shell command on each line of file opened perl script. , file location be, example, design/rtl/name.v
, want result of egrep stored in design/fake/name.v
in shell (bash) make directory results , list files 1 line in file "list" , type straight in @ bash prompt
for in `cat list`;do egrep "string" $i > result/grep${i}; done
here similar in perl. error behaviour nicer, might tiny bit faster: real advantage behaviours added easier. save file , run "perl script list foo" script filename of script, list filename of list of files , foo pattern being searched for
#!/usr/bin/perl # ( $file, $pattern ) = @argv; open( $fh, $file ) || "die $! $file\n"; while (<$fh>) { chomp; #trim newline filename $togrep = $_; open( $grepfh, $togrep ) || warn "$togrep $!"; if ($grepfh) { open( $output, ">result/$togrep" ) || die "$togrep $!"; while ( grep( $pattern, <$grepfh> ) ) { print $output $_; } } }
Comments
Post a Comment