Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to delete every 3rd and 5th line except the 15th line in a text file using sed. Has someone an idea?

share|improve this question
    
Is this an interview question? :) – Richard Holloway Feb 14 '12 at 20:35

Try next command. You will need the GNU version of sed:

sed '15 b ; 0~3 d ; 0~5 d' infile

First check if line is 15th: print and read next one. Otherwise, delete every 3rd line and every 5th line starting with first one.

share|improve this answer

It's much easy if you use awk:

$ awk '(NR%3 && NR%5) || NR==15' input.txt
share|improve this answer
    
I think it should be (NR!=15), OQ said except 15th, not except every 15th. – Rich Homolka Feb 14 '12 at 16:01
    
@RichHomolka Thanks. Fixed – kev Feb 14 '12 at 16:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .