You are here

sed: Deleting all lines between two types of lines

Submitted by Druss on Sat, 2012-01-28 01:17

Today, I made quite an impression on my furniture thanks to incessant contact between it and my illustrious head. This, as usual, was due to my looking for a clean regex to solve my issue while working with text files in Vim. My task was, I initially believed, quite simple: delete all the lines that are sandwiched between two types/patterns of lines. In this case, the top slice of the sandwich consisted of a line which was entirely a number and the bottom slice was a line entirely populated with underscores.

While this sounds quite simple, trying to accomplish it in Vim was an exercise in exercising (exorcising?) my patience :/ Vim's negative modifiers, fancy \_ character classes and what not were all inconsistent (read, I sucked) or too complicated which led to me giving up on an in-editor solution. I decided to turn to my two favourite friends in my times of need: sed and awk. A little googling around and experimentation led to the following elegant solution:

sed '/^[0-9]\{1,\}/,/^_\{2,\}$/d' 1.txt > 2.txt

In other words, sed provides a fracking comma operator - how cool is that?! A neat, clean, nifty, cute and feel-good solution :)

P.S. That said, I really wish all the dudes who are responsible for the myriad regex variants in the world would take the time to talk to each other and decide to either require that we escape all control characters or none at all. WTF is with this half-arsed inconsistency?