awk打印每第n个匹配的行,所有行都不是数学

时间:2017-04-11 17:28:33

标签: bash shell awk

我知道如何打印每个第n行和第n个匹配的行,但不知道如何打印每个第n个匹配行和所有行都不是数学。

输入示例:

Something else 1
Downloading: file1 1%
Downloading: file1 10%
Something else 2
Downloading: file1 30%
Something else 3
Downloading: file1 40%
Downloading: file2 50%
Downloading: file1 60%
Downloading: file1 100%
Downloading: file2 100%
Something else 4

如果模式是' ^正在下载:'并打印每个第二个匹配的行,输出应该是这样的:

Something else 1
Downloading: file1 10%
Something else 2
Something else 3
Downloading: file1 40%
Downloading: file1 60%
Downloading: file2 100%
Something else 4

2 个答案:

答案 0 :(得分:3)

$ awk '!(/Downloading/ && ++c%2)' file
Something else 1
Downloading: file1 10%
Something else 2
Something else 3
Downloading: file1 40%
Downloading: file1 60%
Downloading: file2 100%
Something else 4

答案 1 :(得分:0)

对于perl粉丝:

perl -nlE 'say unless /Downloading/ && ++$n%2'