2017-04-11 60 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'