Shuvo Shams
Shuvo Shams

Reputation: 653

Passing variables from awk to PHP

I am trying to pass back the results/variables from the awk command (called from PHP) back to the PHP script for further manipulation. Following is my script:

$cmd = "/usr/bin/awk '{if($1 >= \"$S_Date\" && $1 <= \"$E_Date\"){sum+=$5; row+=1}} END{avg=sum/row;print avg}' data.txt";
$avg = system($cmd);
echo "Avg: $avg\n";

where, $S_Date and $E_Date are PHP variables. I can get the avg from awk returned in the $avg var but, have the following issues.

Issues:

  1. I want to avoid echoing the avg to the stdout too, from the awk command itself?
  2. How to return/store multiple variables from the awk command, say both sum and avg?

Thanks.

Upvotes: 1

Views: 755

Answers (1)

Mr. Llama
Mr. Llama

Reputation: 20909

You might want to use exec instead.
The second parameter allows you "catch" the output of the command.

exec($cmd_to_run, $output);

Note: If you just need the last line, $last = exec($cmd_to_run); will suffice.

Upvotes: 1

Related Questions