Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Say I have this 857835 line file, containing stuff like this:

a1
rubbish1
rubbish2
rubbish3
rubbish4
a1
rubbish5
rubbish6
rubbish7
rubbish8

And I wish to remove all occurences of a1 and the next line (rubbish1 and rubbish5 in this example). How do I do it?

I've tried grep 'a1' -v -A1 to no avail, and my sed skillz are not really great :}

My Google-fu has not been able to help me this time, someone please help!

share|improve this question
add comment

2 Answers

up vote 15 down vote accepted

Try:

sed -e '/^a1$/,+1d' "filename"

This means from /^a1$/ to the next line, delete

The ^ and $ ensure you match the whole line, so a hidden a1 will not be matched.

share|improve this answer
6  
You might want ^ and $ in that match, to force matching whole lines. –  Jander Feb 9 '11 at 4:16
    
@Jander: Sure, corrected –  asoundmove Feb 9 '11 at 21:44
add comment

The following will work on non-GNU sed (the ,+1 address syntax is a GNU extension):

sed -e '/^a1$/,/^/d' my_file >my_filtered_file

"Starting at a line that reads exactly 'a1', and ending at the next line for which the beginning of the line exists (i.e. the next line), delete."

It's much less extensible than @asoundmove's answer, though, as deleting a different number of lines would take an entirely different script.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.