The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   searching for a select group of control chars (http://hintsforums.macworld.com/showthread.php?t=64236)

Hal Itosis 12-10-2006 08:06 PM

Quote:

Originally Posted by hayne (Post 340920)
I'm not quite sure what is happening but it would seem to be caused by the space characters in your filenames (or is it newline characters in the filenames? - you refer to both these cases) when they are passed to the Perl script as arguments. I.e. I don't think it is the Perl script itself that is having problems but rather that it isn't receiving the filenames properly.

Actually, I refer to your script in post #16... and it handles newlines in
filenames just fine. (It was my script I said had trouble with newlines).

But -if you add a space to the end of some filename- and then run your
script on its parent... perhaps you'll see for yourself what I'm saying.

I'll will try the 'join' test and see what results. Thanks.
I wish Perl was easier to understand. Its syntax is so
"overloaded", but I guess that's also why it's powerful.

:
:

Test complete; filenames are being received correctly;
error messages print first. Is it a compile-time issue?
(No, I guess not... "while (<>)" is simply messing up).

Those perl man pages are amazing -yet- I have a very
hard time following along. How can this be fixed?

hayne 12-10-2006 11:38 PM

One obvious solution to the problem of filenames with spaces at beginning or end is to rename those files so as to avoid this nastiness.
You could do that with the script 'rename_without_endspace' that is available from my Perl script page.

If you don't want to rename your files to avoid whitespace at the ends of the filenames, you could change your Bash script to use the following revised version of my Perl code:
Code:

grepl_perl_code=$(cat <<'EOT'
use warnings;
use strict;
use Fcntl;

die "Usage: grepl pattern file1 [file2 ...]\n" unless scalar(@ARGV) >= 2;
my $pattern = shift @ARGV;

foreach my $filename (@ARGV)
{
    sysopen(FILE, $filename, O_RDONLY)
            or die "Unable to open file \"$filename\": $!\n";
    while (<FILE>)
    {
        if (/$pattern/o)
        { 
            print "$filename\n";  # output name of current file
            seek(FILE, 0, 2); # go on to next file
        }
    }
    close(FILE);
}
EOT)

This revised version of the Perl code makes explicit the looping over command-line arguments and opening of the files that was previously done by Perl's magic "<>" operator. By using 'sysopen', I sidestep any problems with strange characters (like leading or trailing spaces) in the filenames.

Hal Itosis 12-11-2006 12:49 AM

That dood it!
Thanks dude.
:)


All times are GMT -5. The time now is 05:33 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.