简体   繁体   English

如何在循环中打印东西到控制台,以便unix grep可以与它交互?

[英]how to print stuff in a loop to console, so that unix grep can interact with it?

How to print stuff in a loop to console, so that UNIX grep can interact with it ? 如何将循环中的东西打印到控制台,以便UNIX grep可以与它进行交互? I made a console php tool for parsing some data. 我制作了一个用于解析一些数据的控制台php工具。 I am in need of printing some data to console. 我需要将一些数据打印到控制台。

public function printAlerts()
{
    $alertLinks = $this->parser->alertLinks($this->mc->listAlerts());

    $idSize = $alertLinks['sizeArray']['id'];
    $dateSize = $alertLinks['sizeArray']['date'];
    $nameSize = $alertLinks['sizeArray']['name'];

    $margin = 5;

    foreach ($alertLinks['alertArray'] as $alert)
    {
        $this->printColumn($alert['id'], $idSize, $margin);
        $this->printColumn($alert['date'], $dateSize, $margin);
        $this->printColumn($alert['name'], $nameSize, $margin);
        echo "\n";
    }
}

private function printColumn($data, $space = 0, $margin = 0)
{
    echo $data;
    $len = ($space - strlen($data)) + $margin;

    for ($i = 0; $i < $len; $i++)
    {
        echo ' ';
    }

    return;
}

I would like to interact with this printed data with Unix grep command. 我想用Unix grep命令与这个打印数据进行交互。 For example: 例如:

php script.php -list | php script.php -list | grep stuff grep的东西

Using this code all data gets printed in the console, but grep can not filter it, why? 使用此代码,所有数据都会在控制台中打印出来,但是grep无法对其进行过滤,为什么? how to make grep filter the text ? 如何让grep过滤文本?

Consider this demonstration: 考虑这个演示:

<?php
$words = array( "column", "igloo", "magenta", "turnip", "adlib", "stuff");

foreach( $words as $word){
        printf("%8s\n", $word );
}

We can filter out the line containing stuff just fine. 我们可以过滤掉包含stuff的行。

$ php ./t.php | grep 'stuff'
   stuff

If instead we write to stderr , we must filter a bit diferently: 如果我们写入stderr ,我们必须过滤掉一点:

<?php
$words = array( "column", "igloo", "magenta", "turnip", "adlib", "stuff");

foreach( $words as $word){
        fprintf(STDERR,"%8s\n", $word );
}

Since grep reads from stdin, we need to make sure we redirect stderr to stdout : 由于grep从stdin读取,我们需要确保将stderr重定向到stdout

php ./t.php 2>&1 | grep stuff

Finally, if you want to make sure you don't get stdout too, you could switch them: 最后,如果你想确保你没有得到stdout ,你可以切换它们:

php ./t.php 2>&1 1>&2 | grep stuff

Or just discard the data originally written to stdout: 或者只是丢弃最初写入stdout的数据:

php ./t.php 2>&1 >/dev/null | grep stuff

Hope it helps! 希望能帮助到你!

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

相关问题 如何根据内容更改while循环条件? - how to change the while loop condition depending on stuff? 如何捕获UNIX TERM信号,以便可以正常终止PHP进程? - How do I catch a UNIX TERM Signal so I can gracefully terminate a PHP process? 如何使该循环的输出顺序是随机的? - How can I make so that the order of this loop's output is random? 如何在 Laravel 的控制台上打印消息? - How to print messages on console in Laravel? 如何在UNIX时间戳中转换日期值,以便可以使用php的date函数? - How can I convert my date value in a unix timestamp so I can use php's date function? 如何让网站直接连接到打印机? 这样它就可以直接打印出订单 - How can i make a website connect directly to a printer? so that it can print out the orders directly 如何设置for循环以在HTML页面中打印图像 - How can I set my for - loop to print images in the HTML - page 如何将图像存储在数组中并使用 for 循环打印它们? PHP - How can i store images in an array and with a for loop print them? PHP 如何使用 for 循环打印关联数组的元素? - How can I print the elements of an associative array with a for loop? 我可以通过 socket_sendmsg (sendmsg) 将 TCP 套接字资源传递给 AF_UNIX 本地套接字吗?如果可以,如何传递 - Can I pass a TCP socket resource via socket_sendmsg (sendmsg) to a AF_UNIX local socket and if so how
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM