in reply to Perl as grep

I tried perl -p "*.*" -e "print $_ if /regex/" and it's complaining about the *.*.

Aye, that's because you've got it in the wrong place. It looks like you want:

perl -ne "print if /regex/" *.*

or maybe

perl -pe "next unless /regex/" *.*

Though I'm not sure about the globbing under windows. I seem to recall that each program must do its own. Perhaps what you really need is this:

perl -ne "BEGIN { @ARGV = glob(qq(@ARGV)); } print if /regex/" "*.*"

caveat lector! this code is untested.

Replies are listed 'Best First'.
Re^2: Perl as grep (-p/next)
by tye (Sage) on Dec 02, 2003 at 18:36 UTC

    duff++

    Note that one of your examples:

    perl -pe "next unless /regex/" *.*
    doesn't do what you think. -p puts the print statement in the while(<>) loop's continue block and so it will get called when you call next. So that one-liner prints all lines of a file named "*.*" (yes, you are correct about globbing, much to my consternation -- the reasons for this are nonsense), just not as efficiently because it applies a regex to each line and then effectively ignores the result of that test.

                    - tye