【问题标题】:comm command to get common data from file1 file2comm 命令从 file1 file2 获取公共数据
【发布时间】:2015-10-10 01:52:54
【问题描述】:

我想获取 file1 和 file2 中存在的公共数据。

<?php 
    $cmd="comm -12 <(sort /Source/20-07-2015/file1 | uniq) <(sort /Source/20-07-2015/file2 | uniq) > /20-07-2015/commondata_20072015-248932-ac.csv";
    $result =exec($cmd);
 ?>

但是上面的代码抛出了一个错误:

sh: -c: line 0: syntax error near unexpected token `('

【问题讨论】:

  • 你可以试试$result=exec(escapeshellcmd($cmd))
  • /bin/sh 不支持进程替换。您需要使用/bin/bash(可能手动)。
  • deepak 我曾尝试过,但它提供了 comm: extra operand |' Try comm --help' 以获取更多信息。错误
  • Etan 这是 php 代码,请建议我如何在 php 中执行此操作...与我在 shell 中尝试过的相同,如果我执行 ./scrip.sh ....但我该怎么做它在这里..
  • 有没有其他方法可以从 php 中的两个文件中获取公共数据

标签: php linux shell


【解决方案1】:

PHP exec() 使用 shsh 不支持 &lt;(...)

最简单的解决方法(来自知道sh/bash 但不知道PHP 的人)是让sh 调用bash 来运行您的命令:

$cmd="bash -c 'comm -12 <(sort /Source/20-07-2015/file1 | uniq) <(sort /Source/20-07-2015/file2 | uniq) > /20-07-2015/commondata_20072015-248932-ac.csv'";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多