Reputation: 97
Good Day All I'm having a problem getting this command to work on button press I need to search through a file and output the results in comma delimited text. PHP doesnt seem to like the curly brackets so as far as I could read I need to use exec(), but to no avail as the error I recieve is "syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'" I'm still new to php so sorry if the answer is obvious! Any info will be helpful, Thanks
<html>
<body>
<h1>Linux Command Test</h1>
<form method="POST" action="">
<input type="submit" id="submit" name="submit" value="Submit"/>
</form>
<?php
if (isset($_POST['submit']))
{
$output = exec('grep -i hello test.txt | awk -v OFS=, '{\$1=\$1;print}' > newtest.txt');
echo "<pre>$output</pre>";
}
?>
</body>
</html>
Upvotes: 1
Views: 8845
Reputation: 781741
$output = exec('grep -i hello test.txt | awk -v OFS=, '{\$1=\$1;print}' > newtest.txt');
should be:
$output = exec('awk -v OFS=, \'/hello/i {$1=$1;print}\' test.txt > newtest.txt');
You need to escape the quotes, otherwise they delimit the PHP string. You don't need to escape $
inside single-quoted strings in PHP. You shouldn't redirect the awk
output to a file if you want to capture it in a PHP variable.
And there's no need to pipe grep
to awk
, since awk
has built-in pattern matching.
Upvotes: 2
Reputation: 1740
try escaping the $
grep -i hello test.txt | awk -v OFS=, '{\$1=\$1;print}' > newtest.txt
Upvotes: 3
Reputation: 75645
There are bunch of functions you can use in PHP to invoke external applications, exec()
, system()
etc: http://www.php.net/manual/en/ref.exec.php but mind that it's up to administrator to let you use them. On most shared hostings these functions are disabled
Upvotes: 0