How to delete line termination at end of file?

Find, replace, find in files, replace in files, regular expressions

How to delete line termination at end of file?

Postby holti » Wed Oct 26, 2011 11:29 pm

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
holti
Newbie
 
Posts: 3
Joined: Mon Oct 24, 2011 11:18 pm

Re: How to delete line termination at end of file?

Postby Mofi » Thu Oct 27, 2011 12:46 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to delete line termination at end of file?

Postby holti » Thu Oct 27, 2011 3:01 am

Thanks for your answer.
The last line in all files is always blank
Attachments
blank line.png
blank line.png (143.08 KiB) Viewed 1247 times
holti
Newbie
 
Posts: 3
Joined: Mon Oct 24, 2011 11:18 pm

Re: How to delete line termination at end of file?

Postby Mofi » Thu Oct 27, 2011 3:14 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to delete line termination at end of file?

Postby holti » Thu Oct 27, 2011 3:34 am

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.
holti
Newbie
 
Posts: 3
Joined: Mon Oct 24, 2011 11:18 pm

Re: How to delete line termination at end of file?

Postby pietzcker » Mon Oct 31, 2011 12:11 pm

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.
User avatar
pietzcker
Master
Master
 
Posts: 242
Joined: Sun Aug 22, 2004 11:00 pm

Re: How to delete line termination at end of file?

Postby Mofi » Mon Oct 31, 2011 5:52 pm

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"
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions