[grand-rapids-pm-list] Open a file based on content

Justin Denick justin.denick at gmail.com
Fri Mar 2 10:28:28 PST 2007


I have a bunch of files with garbage names, but they can be identified by
their header.
In bash I can say

x=0
pattern="pattern"
files=`grep -l $pattern list-of-files`

for this in $files do;
   if [ $x -eq 0 ]
      sed s/\~/\\r\\n/g $files > name.txt
   else
      sed s/\~/\\r\\n/g  $files > name.txt$x
      x=`expr $x + 1`
   fi
done


But I want learn how to do this in perl
so it would be something like (and please pardon my ignorance)

my $x = 0;
my $pattern = "pattern";

open DH, "path" or die;
my @files = (readdir DH);
my @found = grep($pattern, @files);
for my $source (@found) {
my destination = "$source.new"
   open INPUT, "<$source" or die;
   open OUTPUT ">$destination" or die;
   while (<INPUT>) {
      s/\176/\012\015/g;
      print OUTPUT $_;
      rename $destination, $source;
   }

}




-- 
In vino veritas.
        [In wine there is truth.]
                -- Pliny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/grand-rapids-pm-list/attachments/20070302/dcf8bc4e/attachment.html 


More information about the grand-rapids-pm-list mailing list