Using Linux awk command in php - php

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>

try escaping the $
grep -i hello test.txt | awk -v OFS=, '{\$1=\$1;print}' > newtest.txt

$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.

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

Related

PHP parse Error while executing a lengthy regex shell command from within php

when i try parsing a file by executing this command from within php using shell_exec():
$shellCommand = "cat $filelocn | awk 'BEGIN{RS="<br>"}{$1=$1}1' |sed '/CURRENT/d' ";
echo $shellCommand ;
An error is displayed:
PHP Parse error: syntax error, unexpected '>' in filename.php
i also tried adding \ before ' ie: "cat $filelocn | awk \'BEGIN{RS=\"<br>\"}{$1=$1}1'";
but it again throws error.
How do i resolve this issue ?
Unless you're trying to interpolate variables in your PHP code into your string, you need to escape the dollar signs too:
$shellCommand = "cat \$filelocn | awk 'BEGIN{RS=\"<br>\"}{\$1=\$1}1' |sed '/CURRENT/d' ";
echo $shellCommand;
Specifically, the $1s are causing your parse error. If $filelocn is a PHP variable, you don't need to escape it.

how to execute unix command in php

i am trying to execute unix command in php script like this.
<?php
echo shell_exec('head -n 1 log_list_23072014|awk -F ',' '{print $2}'');
?>
This is the file , trying to get the first column of the first row.
NODE,CGR,TERMID,VMGW,ET
but the error message i am getting
Parse error: syntax error, unexpected 'shell_exec' (T_STRING), expecting ',' or ';'.
cant able to find please help.
The string you've used is not valid, you have to escape single quotes inside your string:
<?php echo shell_exec('head -n 1 log_list_23072014|awk -F \',\' \'{print $2}\'');
You can also use exec()
There was an extra ' within your command. Using your command in a variable can help with identifying errors, and when using the standard exec it's required.
$cmd = 'head -n 1 log_list_23072014 | awk -F , \'{print $2}\'';
echo shell_exec($cmd);
changing it to the format above should work.

decode htmlentities of a file in PHP

How can I convert/decode html entities of a file's contents (XML) in PHP.
I tried to run this on a command line:
perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_1.xml
It works fine running it on command line but when I try to call it within PHP:
system("perl -MHTML::Entities -ne 'print decode_entities($_)' /apps/www/mydir/xmlfiles/p34580600.xml >> /apps/www/mydir/xmlfiles/p34580600_6.xml");
It creates the file but it is empty. I tried to use html_entity_decode but the XML file is just too big. 20megs at least.
Any help or suggestion is greatly appreciated.
Thanks,
try escaping the $ in the system call. It is possible php is looking for a variable $_ since you are using double quotes for the string command.

Execute a shell command through php and display it in browser?

I would like to execute a shell command through php and display it in a browser. Is there anyway to do so?
here is my php code : [test.php]
<?php
$number=$_GET["num"];
$date=$_GET["date"];
$output = shell_exec('egrep -w '2012-09-01|974' /home/myquery_test/log/push.log');
echo "<pre>$output</pre>";
?>
When I run this(test.php) file from browser nothing shows up. But when i change the
$output = shell_exec('ls')
its working fine!! Why isn't the egrep/grep command not working??
The egrep command isn't working, because you're using single quotes as a string constant delimiter: 'egreep -w' <==> 2012-09-01|974' <==> /home/myquery_test/log/push.log' <==Just use double quotes in the string, or as string delimiters OR escape the quotes.
shell_exec('egrep -w \'2012-09-01|974\' /home/myquery_test/log/push.log');
shell_exec('egrep -w "2012-09-01|974" /home/myquery_test/log/push.log');
shell_exec("egrep -w '2012-09-01|974' /home/myquery_test/log/push.log");
And, to avoid not getting the warnings and errors that would have brought this issue to light when testing, set your ini to E_STRICT|E_ALL, and fix the warnings, rather then ignoring them. [teasingly: after you're done with that, you might want to consider accepting some answers]I see you've accepted a lot while I was typing this post up :)
Using variables in your command:
$output = shell_exec("tail -f | egrep '$number.*$date' /var/www/myquery_test/log/push.log");
$output = shell_exec('tail -f | egrep "'.$number.'.*'.$date.'" /var/www/myquery_test/log/push.log');
$output = shell_exec("tail -f | egrep \"$number.*$date\" /var/www/myquery_test/log/push.log");

pipe php function

I want to use php's strip tags in a bash script. I figured I could just cat the html file I want to use and use that input and pipe it into php and then pipe that into something else (sed). Is that possible? I'm not sure exactly how to pipe the output of file.html into the strip_tag function...maybe put it all in a variable? I want the following to keep just the anchor tags...in the following I put in dummy text for strip_tags string because I didn't know how to pipe file.html in:
cat file.html | php strip_tags("<p><a href='#'>hi</a></p>",'<a>') > removed_tags.html
You can read from STDIN in PHP using the stream URI php://stdin. As for executing it, you'll also need to quote the PHP code and use the -r option, as well as echoing the result. So here's the fixed script:
cat file.html | php -r "echo strip_tags(file_get_contents('php://stdin'), '<a>');" > removed_tags.html
Reading from stdin in PHP and writing a php script without a file are possible, but it's way more trouble than just writing a file like
<?php echo strip_tags(file_get_contents($argv[1]), '<a>');
...
$ php that-file.php file.html > removed_tags.html

Categories