[OFF] sed equivalent of something easy in python

Jussi Piitulainen jpiitula at ling.helsinki.fi
Wed Oct 27 10:39:03 EDT 2010


Jussi Piitulainen writes:
> Daniel Fetchinson writes:
> 
> > This question is really about sed not python, hence it's totally
> > off.  But since lots of unix heads are frequenting this list I
> > thought I'd try my luck nevertheless.
> ...
> > 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.
> > 
> > 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.
> > 
> > What would be the sed equivalent?
> 
> The following appears to work here. Both parts of the address are
> documented as GNU extensions in the man page: 2~5 matches line 2 and
> then every 5th line, and ,+1 tells sed to match also the 1 line after
> each match. With -n, do not print by default, and p is the command to
> print when an address matches.
> 
> sed -n '2~5,+1 p'
> 
> Tried with GNU sed version 4.1.2, never used sed this way before.
> 
> So, is there some simple expression in Python for this? Just asking
> out of curiosity when nothing comes to mind, not implying that there
> should be or that Python should be changed in any way.

To expand, below is the best I can think of in Python 3 and I'm
curious if there is something much more concise built in that I am
missing.

def sed(source, skip, keep, drop):

    '''First skip some elements from source,
    then keep yielding some and dropping
    some: sed(source, 1, 2, 3) to skip 1,
    yield 2, drop 3, yield 2, drop 3, ...'''

    for _ in range(0, skip):
        next(source)
    while True:
        for _ in range(0, keep):
            yield next(source)
        for _ in range(0, drop):
            next(source)



More information about the Python-list mailing list