Problem with reformating file

Discussion in 'Programming/Scripts' started by SerZKO, Jan 11, 2018.

  1. SerZKO

    SerZKO New Member

    I have a problem with file which was reformated during some operation and looks like this:
    Code:
    18450 0600060009                       25  0 768188519       768188519       0030020124 000 000   10
    77019           73587                       25  0 73165           730740506       0160100051 029 000
    74049                                       25  0 73950                           1040030047 030 000
    14048           14048                       25  0 11833           11833                      022 000
    18450 0600060027                       25  0 709948778       709948778       0010030064 000 000   10
                    73330                       25  0 0050312875      77020                      000 000
    18450 0600040020                       25  0 703820853       703820853       0030020125 000 000   10
    And I need it to look like this
    Code:
    18450 0600060009                       25  0 768188519       768188519       0030020124 000 000   10
    77019      73587                       25  0 73165           730740506       0160100051 029 000
    74049                                  25  0 73950                           1040030047 030 000
    14048      14048                       25  0 11833           11833                      022 000
    18450 0600060027                       25  0 709948778       709948778       0010030064 000 000   10
               73330                       25  0 0050312875      77020                      000 000
    18450 0600040020                       25  0 703820853       703820853       0030020125 000 000   10
    What happened was that column B has shifted 5 places to the right.
    I've tried to solve it with a simple
    Code:
     while read do printf 
    but the problem is how to read empty spaces into the variable. It looks like sed would be a great tool for that, but I just don't know how to do it as any of column where number of digits is greater then 3 can be empty.
     
  2. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    That format looks very fragile. But here is my attempt. I'm assuming the example contains all cases.
    Code:
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    # Read from standard input, write to standard output.
    # Assume column B (column number 2) is either 10 digits or empty.
    # If column B is not 10 digits remove 5 spaces after column A.
    #
    # Tested on Python 3.4.2 Debian GNU/Linux 8 Jessie.
    # Tapio Lehtonen / Osk Satatuuli 2017
    
    import sys
    debug = False
    
    for str in sys.stdin.readlines():
        if debug: print(">>>", str);
        if str[0].isspace():
            if debug: print("first pos empty")
            #Column A is empty
            #Print slice from char position 5 to end of string
            print(str[5:], end="") # Newline already in original
        elif str[6].isspace():
            if debug: print("pos 7 is empty")
            #Column B is empty or not full 10 digits
            #Print Column A, skip 5 positions and print rest of string
            print(str[:5], end="")
            print(str[10:], end="")
        else:
            if debug: print("Print as is")
            #Print string as is
            print(str, end="")
    
     

Share This Page