LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Executing a awk command inside perl script -- (https://www.linuxquestions.org/questions/linux-newbie-8/executing-a-awk-command-inside-perl-script-894637/)

Alkass 07-30-2011 08:57 AM

Executing a awk command inside perl script --
 
Hello experts

I want to execute a awk command, which reads from txt files and sums the numbers from the first column for those listed only inside a <init> block -- The awk command is like

Code:

awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum}
So, I want to execute it inside a perl script, and execute the awk command for the infile which is also defined outside awk loop, ie doing something like


Code:

foreach $infile (@ARGV) {
  $gzin = gzopen($infile, "r") || die ("Couldn't open file $infile\n");

  # No. events and cross-section from current file
 my $xsec= `awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum}' $infile`;

But this does not work and I get this

Code:

Use of uninitialized value in concatenation (.) or string at merge.pl line 54.
awk: /</?init>/{x = !x}x{a++}x && a > 2{sum+=}END{printf"%E" "
awk:          ^ unterminated regexp
awk: cmd. line:1: /</?init>/{x = !x}x{a++}x && a > 2{sum+=}END{printf"%E" "
awk: cmd. line:1:                                                          ^ unexpected newline or end of string
awk: cmd. line:1: ", sum}
awk: cmd. line:1: ^ unterminated string
Argument "" isn't numeric in numeric eq (==) at merge.pl line 98.

Any ideas ?

grail 07-30-2011 10:33 AM

Quote:

Any ideas ?
Why would you go to another string manipulating command when you are already inside one of the most powerful??

Perl is able to do everything (and of course a hell of a lot more) that is in your awk command.


All times are GMT -5. The time now is 11:21 AM.