Bash-remove-n-match

From I Will Fear No Evil
Revision as of 10:11, 17 November 2021 by Chubbard (talk | contribs) (Created page with "Really unique problem and resolution using awk found in reddit. [https://www.reddit.com/r/bash/comments/qva5bm/delete_n_occurrence_of_a_pattern/?%24deep_link=true&correlation_...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Really unique problem and resolution using awk found in reddit. [1]


input text1.txt:
hi 1
hi 2
hi 3
hi 4
hi 5
hello

I want to delete 4 lines containing 'hi'
wanted output:

hi 5
hello

Unique answer:

awk '!/hi/||++n>4'