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% 5text1text2text3output%1%2text1text2text3I used sed /^%/d file but it deletes all the lines starting with %, sed 3,/^%/d doesn't help either. I need to keep first n lines of the pattern and delete the rest 
Remove lines based on pattern but keeping first n lines that match
text processing;awk;sed
If you want to delete all lines starting with % put preserving the first two lines of input, you could do:sed -e 1,2b -e '/^%/d'Though the same would be more legible with awk:awk 'NR <= 2 || !/^%/'Or, if you're after performance:{ head -n 2; grep -v '^%'; } < input-fileIf 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:awk '!/^%/ || ++n <= 2'With sed, you could use tricks like:sed -e '/^%/!b' -e 'x;/xx/{h;d;}' -e 's/^/x/;x'That is, use the hold space to count the number of occurrences of the patterns matched so far. Not terribly efficient or legible.