简体   繁体   中英

AWK is not producing an output when used in PHP

<?php 
$dd="/root/report/lastDate-28-11-2013.csv";
$c_row=exec("awk 'BEGIN { FS = \",\" } ; { print $1 }'" .$dd);
//echo $c_row;
print_r($c_row); 
?>

When i run this code it simply hangs and does not print any output.

Have a look at the manual , you're missing an output and/or return_var parameter.

To get the output of the executed command, be sure to set and use the output parameter.

Syntax:

string exec ( string $command [, array &$output [, int &$return_var ]] )

output

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \\n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

return_var

If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

In your case, it might look something like...

$csv = "/your/file.csv"; 
$comm = "awk 'BEGIN { FS = \",\" } ; { print $1 }'" .$dd;
exec($comm, $output);
var_dump($output);

(I haven't checked your awk command so far, I suspect it returns the desired result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM