[OFF] sed equivalent of something easy in python

Daniel Fetchinson fetchinson at googlemail.com
Mon Oct 25 19:04:00 CEST 2010

>> using python. The pattern is that the first line is deleted,
>> then 2 lines are kept, 3 lines are deleted, 2 lines are kept,
>> 3 lines are deleted, etc, etc.
> If you have GNU sed, you can use
>    sed -n '2~5{N;p}'
> which makes use of the GNU "~" extension. If you need a more
> portable version:
>   sed -n '1d;N;p;N;N;N;d'
> Both have the side-effect that the expect the printed lines to
> come in pairs, so if you have
>   seq 17 | sed -n '...'
> it won't print the 17, but if you take it to 18, it will print 17
> and 18.  To address that (so to speak), you can use
>   sed -n '1d;p;n;p;N;N;N;d'

Thanks a lot, Tim!

>> But I couldn't find a way to do this with sed and since the
>> whole operation is currently done with a bash script I'd hate
>> to move to python just to do this simple task.
> I'm not sure this is a great reason to avoid Python, but whatever
> floats your boat :)

Well, the reason I wanted to avoid python in this particular case is
that I have a large bash script that does its job perfectly and I
needed to insert this additional task into it. I had 3 choices: (1)
rewrite the whole thing in python (2) add this one task in python (3)
add this one task in sed. I chose (3) because (1) looked like a waste
of time and (2) made me take care of 2 files instead of 1 from now on.


Psss, psss, put it down! - http://www.cafepress.com/putitdown

More information about the Python-list mailing list