Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

trying to convert from awk to perl

by Anonymous Monk
on Jan 09, 2006 at 16:37 UTC ( #521986=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am very new to perl and truthfully to awk. I have an awk statement that works wonderfully for 11 files I have to parce. But I cannot seem to convert this to either A) work inside of Perl or B) figure out how to write it in Perl. I have tried using a2p but it makes no sense to me.

Here is the awk statement.

awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak > /tmp/frhtest/ADDC. +$date
It simply reads column 1 for anything that starts with "F" and copies that into another file.

I tried using system call

$call = `awk '$1 ~ /^F/ {print $0 }' /tmp/frhtest/ADDC.bak > /tmp/frht +est/ADDC.$date`; system("$call");
but I get the following error.
syntax error The source line is 1. The error context is >>> ~ <<< awk: Quitting The source line is 1.
Any help would be greatly appreciated.

John

Comment on trying to convert from awk to perl
Select or Download Code
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 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 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 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://521986]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-08-30 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls