How to delete line termination at end of file?

How to delete line termination at end of file?

3
NewbieNewbie
3

    Oct 27, 2011#1

    I have a folder with many files. Every morning I must open all files and delete the line break at the end, then save it.
    How can I automatically delete line breaks at the end of a file? The file has many line breaks but only the line break at the end has to be deleted.
    Thanks for an answer

    6,675585
    Grand MasterGrand Master
    6,675585

      Oct 27, 2011#2

      It is not possible to search for a string at end of file with any regular expression engine supported by UltraEdit. There is no "end of file" anchor supported. Therefore you can't use a simple replace in files command to remove the last line termination, except the last line in all files contains always the same string not existing anywhere else in the file like </html> for HTML files.

      It would be possible to use a script or a macro to open all files within a folder, go to end of each file, delete the last line termination in each file and save the files.

      For example you call UltraEdit with a shortcut having following command line:

      "Path to UltraEdit program files folder\uedit32.exe" /fni "Path to folder with the files to modify\*.*" /M,E="path to macro file\DeleteLastLineTerm.mac"

      The macro file DeleteLastLineTerm.mac contains just 1 macro with following code:

      Code: Select all

      Loop 0
      IfNameIs ""
      ExitLoop
      EndIf
      Bottom
      Key BACKSPACE
      CloseFile Save
      EndLoop
      But a macro using Replace In Files would be much faster when all files end always with the same string because opening lots of files makes the macro solution very slow in comparison to usage of a macro with just command ReplInFiles.

      3
      NewbieNewbie
      3

        Oct 27, 2011#3

        Thanks for your answer.
        The last line in all files is always blank
        blank line.png (143.08KiB)

        6,675585
        Grand MasterGrand Master
        6,675585

          Oct 27, 2011#4

          Well, to be precise, according to your image the last line in all files has a line termination which is absolutely correct and normally highly recommended. In your example the last line of the file is dok_dat_feldt[17] = "EUR". If there would be no line termination here, this would be just a string at end of file and not a line at end of file. The line where caret is positioned on your image is not really a line, but text editors display them as line so that text writers can append additional lines easily.

          However, if you want to remove last line termination for some reason I don't know and which I don't need to know, you can use the macro solution as I have suggested already. There are other solutions if this one does not work. There is obviously not always the same string in last line of all files, so a quicker Replace in Files can't be used most likely.

          3
          NewbieNewbie
          3

            Oct 27, 2011#5

            Hi,

            I have to remove it because the files are created via IBM AS400 and our software for archiving can't archive these files automatically due to the line break in each file.

            236
            MasterMaster
            236

              Oct 31, 2011#6

              It is true that UE doesn't support the end-of-file anchor (\Z). But you can simulate it using a negative lookahead assertion. Search (using Perl regular expressions) for

              Code: Select all

              (?s)\r\n(?!.)
              and replace with nothing.

              This regex matches a newline (\r\n) only if it is not followed by any other character ((?!.) effectively means "end of file" since that's the only position where no character follows). The (?s) at the start tells the regex engine to allow the dot to match any character including newlines.

              6,675585
              Grand MasterGrand Master
              6,675585

                Oct 31, 2011#7

                Thanks, Tim. That is really a great expression and works fine.

                holti, you can now use a macro which runs a Perl regular expression Replace In Files on all files in a specified folder without loading all files in this folder.

                The macro code stored in a macro file named DeleteLastLineTerm.mac is:

                PerlReOn
                ReplInFiles RegExp Log "
                Path to folder with the files to modify ending with a backslash" "*" "(?s)\r\n(?!.)" ""

                The command line of a shortcut or in a batch file needed to run this macro is:

                "Path to UltraEdit program files folder\uedit32.exe" /fni /M,E="path to macro file\DeleteLastLineTerm.mac"