[OFF] sed equivalent of something easy in python

Tim Chase python.list at tim.thechases.com
Mon Oct 25 12:52:29 EDT 2010


On 10/25/2010 11:25 AM, Daniel Fetchinson wrote:
> 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'

> 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 :)

However, if you have further sed questions, the sed mailing list 
over at Yahoo! Groups is a friendly one and will keep the noise 
down here.

-tkc






More information about the Python-list mailing list