当行不断重复时终止linux程序

时间:2018-11-20 16:37:14

标签: bash awk pipe infinite-loop

我正在处理的财务处理程序 cruncher.js 中有烦人的bug,难以解决。一个常见的问题(其触发输入难以确定要点,因此可以避免)导致此失败情况:

Downloading account information...
Downloading today orders...
Downloading historical quotes...
Downloading historical quotes...
Downloading historical quotes...
Downloading historical quotes...

一旦重复行“正在下载历史报价...” 第三遍,我知道它会遇到无限循环且永不退出,也不会跳过无法正常处理的任何输入情况。

我如何将这个cruncher.js程序传递给 | awk ,即哪种内联awk脚本会在其输入中检测到第3条(或第2条,如果更容易的话)连续的重复行,并在那里终止?

或者使用其他常见的Linux / shell工具代替awk?

1 个答案:

答案 0 :(得分:2)

这是一种方法

$ yes | awk -v key='y' '{if($0==key)c++; else c=0} c==3{exit}1' 
y
y

用重复的值替换键值;和yes与您的流生成器​​一起使用。