Here. Reads from stdin and writes to stdout. Should do what you want.
Don't forget to make it executable. Save to a file, e.g. "type_munger.py"
then:
chmod +x type_munger.py
then:
./type_munger.py < input_file.txt > output_file.txt

Your line terminators, whatever they are (\n or \r\n or \r) are
stripped off and whatever line terminators are native to the system
you are running the script on will be used.

#!/usr/bin/env python

from sys import stdin

lines = stdin.readlines() # suck up all the lines
state = 'normal'
for line in lines:
    line = line.rstrip() # Strip whitespace off the end
    if line == "Type=0x21":
        state = "seen 0x21"
        continue
    if state == "seen 0x21":
        if line[9:11] == "00":
            print "Type=0x22"
        else:
            print "Type=0x21"
        state = 'normal'
    print line



On 25/10/05, Tom Munro Glass <[EMAIL PROTECTED]> wrote:
> I've got several large files that require some repetitive multiline search and
> replace operations, and I feel sure that sed can do this, but I can't figure
> out how to do it.
>
> The pattern I'm searching for in the input file is "Type=0x21" followed by
> "Label=abcde" on the next line, where abcde is a 5 character number with
> leading spaces. If abcde is a multiple of 100, (d=0, e=0) I want to replace
> the first line with "Type=0x22", and add a third line "EndLevel=1"
>
> For example:
>
> BEFORE:                 AFTER:
> Type=0x21               Type=0x21       # No change required because
> Label=  120             Label=  120     # Label not multiple of 100
> blah, blah              blah, blah
>
> Type=0x21               Type=0x22       # Change Type value
> Label=  100             Label=  100
> blah, blah              EndLevel=1      # Insert extra line
>                         blah, blah
>
> Type=0x21               Type=0x22       # Same as previous except
> Label= 2200             Label= 2200     # different value for Label
> blah, blah              EndLevel=1
>                         blah, blah
>
> The bit that's got me stumped is how to handle the Label line. Can someone
> please give me hint how to do this?
>
> Tom
>

Reply via email to