regextextlf

Add new line after x charters (repair corrupted file)


I have a text file from a datalog, the file should be lines of data, space padded so that each line is 197 characters long and terminated with LF. Some of the LFs are missing, so the next line is conjoined to the previous line. E.g. line 1 is good, line 2 is missing the LF so line 3 is joined to it (and a few more lines!):

IO.Unit._1.BottomBoard.Iin._1.AmpsA                                             ,Current A Pump 1                        ,29,57.80                                   ,0001641474794.344049,0000356036
PumpControl.Pump._1.PumpReverse.Reversing                                       ,Pump 1 Reversing                        ,28,2875                                    ,0001641474794.026816,0000356035Faults.Pump._1.ThermalOverload.Status.Active                                    ,Pump 1 Tripped                          ,24,1                                       ,0001641474793.000000,0000356034Faults.Pump._1.NotInAuto.Status.Active                                          ,Pump 1 Unavailable                      ,24,1                                       ,0001641474793.000000,0000356033Faults.Pump._1.Flow.LowFlowFault.Status.Active                                  ,Pump 1 Low Flow                         ,24,2                                       ,0001641474792.000000,0000356032Faults.Pump._1.Flow.LowFlowFault.Status.Active                                  ,Pump 1 Low Flow                         ,24,4                                       ,0001641474792.000000,0000356031Faults.Pump._1.Flow.LowFlowFault.Status.Active                                  ,Pump 1 Low Flow                         ,24,3                                       ,0001641474734.000000,0000355950IO.Unit._1.BottomBoard.Iin._1.AmpsB                                             ,Current B Pump 1                        ,29,0.00                                    ,0001641474724.555403,0000355949

Is there a simple way to find when character 198 is not a LF, and insert an LF? I've tried RegEx search/replace in NotePad++:

Find: (.{197}!\r)

Replace with: $1\r\n

This returns no hits. If I don't have !\r, it finds every line (because they're all 197 chars long).

I have been manually adding the LF, but I think I have a few thousand such lines... in this file alone... !! So I'd REALLY like to automate this.

I can use RegEx to:

Find: (.{197})

Replace with: $1\r\n

But this appends \n to every good line. I can just use NotePad++ remove blanks lines, but I was wondering if there was a smarter way.

Thanks for your help!


Solution

  • As the comments mentioned, using an actual programming language would be more reliable than the regex. However, here's a regex that should work if you're unable to use a programming language.

    (.{197})(?!(?:\r|\n))
    

    Demo

    (.{197})      - Grab the first 197 characters of the line
    (?!(?:\r|\n)) - negative lookahead. If the 198th character is a \n or \r it won't match
    

    Anchoring Issues

    The regex isn't anchoring on the beginning of the line, and counting 197 characters from the beginning. If you wanted to anchor from the beginning of the line and capture all records in the line you'd need to do something like this:

    ^(?:(.{197})(?!(?:\r|\n))){1,}
    

    Problem with using this version is the repeating group syntax {1,} isn't commonly supported in a "replace" regex.