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

hi,

I have a regular command that runs fine from the command line.

find . -type f|xargs grep "Login*" 2>/dev/null | grep "2006-03-08" | p +erl -nae 'print "$F[0] $F[8]\n"'

When I run the above command I can get the 1st and 8th column.

But when I put the command in a script, the command doesnt work. Please give any suggestions.

Thank you

Edited by planetscape - added code tags

Replies are listed 'Best First'.
Re: perl and awk
by graff (Chancellor) on Mar 09, 2006 at 06:52 UTC
    Why do you refer to "awk" in the title of your post?

    When you say "put the command in a script", do you mean that you have a perl script that contains something like this?

    my $cmd = <<CMD; find . -type f | xargs grep "Login*" 2>/dev/null | grep "2006-03-08" | + perl -nae 'print "$F[0] $F[8]\n"' CMD system( $cmd );
    If so, I'm sure there is a better way to do whatever you're trying to accomplish. As it is, you are probably getting fouled up by improper or inadequate escaping of "magic" characters and/or some unexpected limits on "quoting within 'quoted' strings". But I'm only guessing, because the only diagnostic info you gave us is "the command doesn't work". (I know from experience that mixing magic characters and multiple layers of quoting within a system call is awfully risky and prone to failure.)

    Here's a suggestion: give us more detail about what you actually did, and about what is really meant by "doesn't work" -- e.g. the error messages and or output that you get. (While you're at it, learn about using <code> tags in your posts.)

    BTW, you said that  print "$F[0] $F[8]\n" gives you the "1st and 8th" column -- I think you meant the "1st and 9th".

Re: perl and awk
by planetscape (Chancellor) on Mar 09, 2006 at 06:15 UTC
Re: perl and awk
by ikegami (Patriarch) on Mar 09, 2006 at 05:42 UTC

    I'm not sure what you're asking, so I'm gonna venture a guess.

    @F is only populated when the -a option is used, and -a must be used in conjunction with -n or -p. None of these are particular useful for scripts. If you want to do split a string into columns, remember that
    perl -nae '...'
    is equivalent to
    perl -e 'while (<>) { @F = split(" "); ... }'

    Does that help?

    Perl command lin switches are documented in perlrun.
    split is documented in perlfunc.

    PS - When posting code here, put <c>..</c> tags around the code. It makes it more readable, escapes special characters, and allows wrapping.