fireartist has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I wish to process a file with a short command line script (*nix).
I don't have experience of running perl from the command line like this.
After reading perl --help I tried.
perl -i ./access_log -e 'while (<>) { #stuff# }' but from the error messages I could tell that perl was trying to execute the file 'access_log'.
I then tried
less access_log > perl -e 'while (<STDIN>) { #stuff# }' but from the output could tell that the file wasn't being read.

I've had a search around here, but can't find the answer.
Could someone shed some light on this?
Thanks

Replies are listed 'Best First'.
Re: process file via command line
by broquaint (Abbot) on Nov 11, 2002 at 17:31 UTC
    Try this instead
    perl -i -pe '# your code here' access_log
    See perlrun for more info about munging from the command line.
    HTH

    _________
    broquaint

Re: process file via command line
by robartes (Priest) on Nov 11, 2002 at 17:28 UTC
    You want to do this:
    cat access.log | perl -ne '#stuff#'
    Or, if you want to print the result after every line:
    cat access.log | perl -nep '#stuff#'
    The -n switch puts the while (<>) loop around your input file for you: see perlrun.

    And, to really spoil you for choice, Perl is smart enough to decide where its input is coming from, so you can do this as well:

    perl -ne '#stuff#' access.log

    CU
    Robartes-

Re: process file via command line
by Enlil (Parson) on Nov 11, 2002 at 17:28 UTC
    you are probably looking for something like the -n or -p option. Like
    perl -ni.bak -e 'stuff' input_file
    There is a good reference as to the use of these perlrun in the documentation.

    -enlil

Re: process file via command line
by Bird (Pilgrim) on Nov 11, 2002 at 17:33 UTC

    You'll probably be interested in the command line switches -p, -n, and -l... These give various levels of convenience when dealing with a file from the command line (see perlrun). You'll then want to use -e, as you've done, but follow the quoted code with the filename you're working on, so that it's fed to your script as a parameter.

    perl -ne 'print unless /^#/' myfile.log
    -Bird
Re: process file via command line
by fireartist (Chaplain) on Nov 12, 2002 at 08:39 UTC