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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: trying to convert from awk to perl
by merlyn (Sage) on Jan 09, 2006 at 16:40 UTC
Re: trying to convert from awk to perl
by explorer (Chaplain) on Jan 09, 2006 at 16:44 UTC
    awk '$1 ~ /^F/ { print $0 }' /tmp/frhtest/ADDC.bak > /tmp/frhtes +t/ADDC.$date perl -ane 'print if $F[0] =~ /^F/' /tmp/frhtest/ADDC.bak > /tmp/frhtes +t/ADDC.$date
Re: trying to convert from awk to perl
by smokemachine (Hermit) on Jan 09, 2006 at 17:34 UTC
    perl -ne 'print if /^F/' /tmp/frhtest/ADDC.bak > /tmp/frhtest/ADDC.$da +te
Re: trying to convert from awk to perl
by Perl Mouse (Chaplain) on Jan 09, 2006 at 22:52 UTC
    The problem you are suffering from is that fact that the backticks are an interpolative context. Which means that the $1 you have is interpolated before the call to awk is made. And you have the same for the $0. Putting backslashes before the dollar signs will solve this problem.

    However, it doesn't make sense to use backticks here. Backticks run the command, and return the output of the command - however, since your command redirects all output, there's nothing to collect. The following system command you make doesn't make sense - it executes nothing. You'd be better off to use system here, instead of the backticks - and then you can q and avoid $1 and $0 to be interpolated:

    system q {awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak > /tmp/frh +test/ADDC.}.$date; die "Command failed" if $?;
    Perl --((8:>*
Re: trying to convert from awk to perl
by runrig (Abbot) on Jan 09, 2006 at 17:47 UTC
    Your awk program is more verbose than it needs to be. The default action is to print $0, and since all you're doing is matching $1 at the beginning of the string, you may as well be matching $0. So your awk program could just be:
    awk '/^F/' # Correction...should be awk '$1 ~ /^F/'
    And actually, there's no reason you couldn't just use grep for this.

    Update: ambrus is correct below. Oops.

      and since all you're doing is matching $1 at the beginning of the string, you may as well be matching $0.

      That's not true, the line

      Foo
      will not match /^F/ but as the leading space doesn't get into $1, $1 ~ /^F/ will be true.
Re: trying to convert from awk to perl
by thor (Priest) on Jan 10, 2006 at 05:01 UTC
    In general, the program a2p that ships with perl is a good first step for converting awk to perl...

    thor

    The only easy day was yesterday