grep invert search with context
If the lines are all unique you could grep the lines you want to remove into a file, and then use that file to remove the lines from the original, e.g. grep -C 2 “line I don’t want” < A.txt > B.txt grep -f B.txt A.txt