Pipe multiple commands to a single command with no

2019-07-14 02:29发布

How can I pipe the std-out of multiple commands to a single command? Something like:

(cat my_program/logs/log.*;tail -0f my_program/logs/log.0) | grep "filtered lines"

I want to run all the following commands on a single command-line using pipes and no redirects to a temp file (if possible). There is one small nuance that means I can't use parentheses; I want the last command to be a tail feed so I want the grep to happen after every line is received by the std-in - not wait for EOF signal.

3条回答
做个烂人
2楼-- · 2019-07-14 03:19

If the commands don't have to be executed in parallel then you can use cat to unify the output into a single consecutive stream, like this:

tail -0f log.0 | cat - log.* | grep "filtered lines"

The magic bit is the - parameter for cat; it tells cat to take stdin and add it to the list of inputs (the others, in this case, being logfiles) to be concatenated.

查看更多
聊天终结者
3楼-- · 2019-07-14 03:27

Try using the current shell instead of a subshell:

{ cat file1; tail -f file2; } | grep something

The semi-colon before the closing brace is required.

查看更多
女痞
4楼-- · 2019-07-14 03:28

Okay I have a messy solution (it's a script - sadly this would be unpractical for the command line):

#!/bin/bash  

ProcessFIFO(){
    while [[ -p /tmp/logfifo ]]; do
    grep "filtered lines" < /tmp/logfifo
    done
}
OnExit(){
    rm -f /tmp/logfifo
    echo 'temp files deleted'
    exit
}
    # Create FIFO
    mkfifo /tmp/logfifo
    trap OnExit SIGINT SIGTERM
    ProcessFIFO &

    cat log.* > /tmp/logfifo
    tail -0f log.0 > /tmp/logfifo

I have used to FIFO method suggested by @itsbruce and I've also had to use a while loop to stop the grep from ending at the EOF signal:

while [[ -p /tmp/logfifo ]]

I have also included a trap to delete the FIFO:

trap OnExit SIGINT SIGTERM
查看更多
登录 后发表回答