2017-04-11 57 views
3

我知道如何打印每第n行和第n个匹配行,但不知道如何打印每第n个匹配行并且所有行都不是数学。awk打印每第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

显示输入片段nd预期输出 – RomanPerekhrest

回答

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

好东西!对于标题:“打印**每秒**线,匹配'/下载/'” – RomanPerekhrest

+1

@EdMorton它的作品。谢谢! – user2914846

0

用于Perl球迷:

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