[grand-rapids-pm-list] Open a file based on content
Justin Denick
justin.denick at gmail.com
Fri Mar 2 10:30:01 PST 2007
On 3/2/07, Justin Denick <justin.denick at gmail.com> wrote:
>
> 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
add one to $x here
x=`expr $x + 1`
else
> sed s/\~/\\r\\n/g $files > name.txt$x
>
> 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
--
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/4ae31aca/attachment.html
More information about the grand-rapids-pm-list
mailing list