I need to remove lines from a text file based on pattern but I need to keep the first n lines that match the pattern.
Input
% 1
% 2
% 3
% 4
% 5
text1
text2
text3
output
%1
%2
text1
text2
text3
I used sed /^%/d
file but it deletes all the lines starting with %, sed 3,/^%/d
doesn't work either. I need to keep first n lines of the pattern and delete the rest
Best Answer
If you want to delete all lines starting with % put preserving the first two lines of input, you could do:
Though the same would be more legible with
awk
:Or, if you're after performance:
If you want to preserve the first two lines matching the pattern while they may not be the first ones of the input,
awk
would certainly be a better option:With
sed
, you could use tricks like:That is, use the hold space to count the number of occurrences of the patterns matched so far. Not terribly efficient or legible.