简体   繁体   English

在php中使用Linux awk命令

[英]Using Linux awk command in 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! 就我所知,PHP似乎不喜欢花括号,但我需要使用exec(),但无济于事,因为我收到的错误是“语法错误,意外的T_LNUMBER,期待的T_VARIABLE或'$'”还是php的新手,如果答案很明显,对不起! 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>

尝试转义$

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. 您需要对引号进行转义,否则它们将界定PHP字符串。 You don't need to escape $ inside single-quoted strings in PHP. 您无需在PHP中的单引号内转义$ You shouldn't redirect the awk output to a file if you want to capture it in a PHP variable. 如果要将awk输出捕获到PHP变量中,则不应将其重定向到文件。

And there's no need to pipe grep to awk , since awk has built-in pattern matching. 而且,由于awk具有内置模式匹配,因此无需将grep传递给awk

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. 您可以在PHP中使用一堆函数来调用外部应用程序, exec()system()等: http : //www.php.net/manual/en/ref.exec.php,但请注意,这取决于管理员让您使用它们。 On most shared hostings these functions are disabled 在大多数共享主机上,这些功能被禁用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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