Search code examples
pythonpython-2.5

Remove amount of lines above and below a line containing a string


I'm formatting GPS output logs and I need an efficient method to remove x number of lines above the line that contains a 0 and y number of lines below that line.

*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 0
     Satellites Used: 3
        MSL Altitude: 00.0
*--------------------------------------*

If the line contains "Position fix ind: 0", remove 6 lines above it and remove 3 lines below in and remove the line it is in

EDIT:

The input file is a .log file

EDIT 2:

input file

1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 0
     Satellites Used: 3
        MSL Altitude: 00.0
 *--------------------------------------*
3
2
1
1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 5
     Satellites Used: 3
        MSL Altitude: 00.0
*--------------------------------------*
3
2
1

Solution

  • def remLines(infilepath, outfilepath, delim, above, below):
        infile = open(infilepath)
        outfile = open(outfilepath, 'w')
        buff = []
        line = infile.readline()
        while line:
            if line.strip() == delim:
                 buff = []
                 for _ in range(below): # need to error check here, if you're not certain that your input file is correctly formatted
                     infile.readline()
            else:
                if len(buff) == above:
                    outfile.write(buff[0])
                    buff = buff[1:]
                buff.append(line)
            line = infile.readline()
        outfile.write(''.join(buff))
    
    if __name__ == "__main__":
        remLines('path/to/input', 'path/to/output', "Position fix ind: 0", 6,3)
    

    Testing:

    Input:

    1
    2
    3
    *--------------------------------------*
                UTC Time: 000000.00
               Latitude: 0000.0000
                N/S ind.: N
               Longitude: 0000.0000
             E/W ind: E
        Position fix ind: 0
         Satellites Used: 3
            MSL Altitude: 00.0
     *--------------------------------------*
    3
    2
    1
    1
    2
    3
    *--------------------------------------*
                UTC Time: 000000.00
               Latitude: 0000.0000
                N/S ind.: N
               Longitude: 0000.0000
             E/W ind: E
        Position fix ind: 5
         Satellites Used: 3
            MSL Altitude: 00.0
    *--------------------------------------*
    3
    2
    1
    

    Output:

    1
    2
    3
    3
    2
    1
    1
    2
    3
    *--------------------------------------*
                UTC Time: 000000.00
               Latitude: 0000.0000
                N/S ind.: N
               Longitude: 0000.0000
             E/W ind: E
        Position fix ind: 5
         Satellites Used: 3
            MSL Altitude: 00.0
    *--------------------------------------*
    3
    2
    1