Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

perl awk

by jarwulf (Initiate)
on Apr 04, 2013 at 23:25 UTC ( #1027046=perlquestion: print w/replies, xml ) Need Help??
jarwulf has asked for the wisdom of the Perl Monks concerning the following question:

hi, I ran across this line in a perl file Does anyone have an idea of what its doing? It seems to be printing out if some comparisons are met is as far as I figure. Also what is with a variable name like temp_marker_$name? Aren't scalar variables supposed to start with $ and not have them in the middle? What is the purpose of the slashes in the awk command?

system ("awk \'\$1 == \"$label\" && \$3 <= $apex && \$4 >= $apex\{print\}\' $reference >temp_marker_$name") ==0 or die "$0 failed to awk";

Replies are listed 'Best First'.
Re: perl awk
by Loops (Curate) on Apr 04, 2013 at 23:39 UTC

    The perl code is calling out to the shell with the system command. It's using interpolation to inject the value of the perl variables $label, $apex, $reference, and $name into the string that will be executed by the shell.

    The output of the awk command will be captured in a file named temp_marker_$name, where $name is first expanded to whatever is held in that perl variable

    The purpose of the the backslashes in the string is to ensure that the following character is passed literally to the shell. For example, this prevents perl from treating constructs such as "$3" as a perl variables.

Re: perl awk
by marto (Bishop) on Apr 04, 2013 at 23:42 UTC

    \ is being used as an escape character, the variables are being used insert values within parts of the strings (see String_interpolation). Basically the program is building a command which gets called by system. You can output the command to screen easily by replacing the system call with a print, like so

    print "awk \'\$1 == \"$label\" && \$3 <= $apex && \$4 >= $apex\{print\ +}\' $reference >temp_marker_$name";
Re: perl awk
by hdb (Prior) on Apr 05, 2013 at 06:59 UTC

    Using Perl instead of awk you would do:

    use strict; use warnings; my $label = "some string"; my $apex = 10; # some number my $reference = "somefile.txt"; my $name = "out.txt"; open IN, "<", $reference or die "Cannot open file $reference.\n"; open OUT, ">", "temp_marker_$name" or die "Cannot open file temp_marke +r_$name.\n"; while(<IN>) { my @fields = split; # cannot remember what awk splits on by defaul +t if( $fields[0] eq $label and $fields[2] <= $apex and $fields[3] >= $apex ) { print OUT $_; } } close OUT; close IN;
      So basically its a loop? Is the comparison being done on every line in reference, a single line, or some other part?
        Pretty much. Awk works a lot like "perl -ane", looping over lines in a file and splitting them into fields. An awk command is of the form "TEST { ACTION }", where TEST controls whether ACTION is performed. It's more involved than that, but since I learned Perl first, I rarely use awk. You might try feeding the awk command to a2p to get a perl equivalent of what it does.
Re: perl awk
by Rahul6990 (Beadle) on Apr 05, 2013 at 05:44 UTC
    Here the awk is processing some file line by line and while reading that file ,it is storing current line's first , third and fourth column in $1 ,$3 and $4 variables. These all are awk's built in variables whose values are initialized automatically.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027046]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2016-12-10 19:12 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (164 votes). Check out past polls.