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:
- I want to avoid echoing the avg to the stdout too, from the awk command itself?
- How to return/store multiple variables from the awk command, say both sum and avg?
Thanks.
You might want to use exec instead.
The second parameter allows you "catch" the output of the command.
Note: If you just need the last line,
$last = exec($cmd_to_run);will suffice.