无法使用 awk 管道输出

use*_*456 8 command-line awk

该函数awk '!seen[$0]++'打印输入管道中的新出现。

但是,我无法在之后添加管道。例如

my_function_with_ouput | awk '!seen[$0]++' | while read j
do
echo $j
done
Run Code Online (Sandbox Code Playgroud)

不产生任何输出。

hee*_*ayl 8

awk缓冲 STDOUT 流,您需要刷新 STDOUT,使用fflush()GNU的功能使其刷新流awk

my_function_with_ouput | awk '!seen[$0]++ {print; fflush()}' | while ...
Run Code Online (Sandbox Code Playgroud)

如果您没有gawk,请stdbuf从无缓冲标准输出中获取帮助:

my_function_with_ouput | stdbuf -o0 awk '!seen[$0]++' | while ...
Run Code Online (Sandbox Code Playgroud)

或使 STDOUT 行缓冲:

my_function_with_ouput | stdbuf -oL awk '!seen[$0]++' | while ...
Run Code Online (Sandbox Code Playgroud)