如何将进程的输出传递给logger命令和head命令?

时间:2020-11-27 14:08:35

What I'm trying to do is:

我想要做的是:

  • Start a process that outputs text continously

    启动一个连续输出文本的过程

  • Pipe that output to two commands:

    管道输出到两个命令:

    1. A logger script
    2. 记录器脚本

    3. 'head' command, so I can save the first lines the initial process output.
    4. 'head'命令,所以我可以保存初始进程输出的第一行。

What I tried so far (unsuccessfuly) is:

到目前为止我尝试过的(不成功)是:

./myProgram | tee > (myLogger log.txt) | head > firstLines.txt

./myProgram | tee>(myLogger log.txt)| head> firstLines.txt

The problem is that the myProgram exits as soon as head is finished.

问题是,头部完成后myProgram会立即退出。

Even if I use -i in tee command, I can't get myProgram to keep running.

即使我在tee命令中使用-i,也无法让myProgram继续运行。

Since the logger may append the incoming text to an existing file, executing head log.txt > firstLines.txt won't work in this case.

由于记录器可能会将传入的文本附加到现有文件,因此在这种情况下执行head log.txt> firstLines.txt将不起作用。

2 个解决方案

#1


1  

You can use awk as an alternative to do both:

您可以使用awk作为两者的替代方法:

./myProgram | 
    awk 'NR<=10{print > "firstLines.txt"} NR>10{close("firstLines.txt")} 1' > log.txt

#2


1  

Like this maybe:

也许这样:

yes | awk 'FNR<4 {print >>"file"; close("file")}  1' | more

where yes is your program, file is where you send the output of head to, and more is your logger.

如果是您的程序,则文件是您将头部输出发送到的位置,更多是您的记录器。

#1


1  

You can use awk as an alternative to do both:

您可以使用awk作为两者的替代方法:

./myProgram | 
    awk 'NR<=10{print > "firstLines.txt"} NR>10{close("firstLines.txt")} 1' > log.txt

#2


1  

Like this maybe:

也许这样:

yes | awk 'FNR<4 {print >>"file"; close("file")}  1' | more

where yes is your program, file is where you send the output of head to, and more is your logger.

如果是您的程序,则文件是您将头部输出发送到的位置,更多是您的记录器。