Link to home
Start Free TrialLog in
Avatar of Mohonk
MohonkFlag for United States of America

asked on

Remove blank lines at the end of a text file

I have a txt file that gets uploaded to us on a nightly basis.  This file is processed by one of our systems.  On occasion this file has 2 or 3 blank lines at the very end.  This causes issues when being processed.  Need a way to remove these blank lines after it is uploaded before it gets processed.   I'd prefer some sort of dos batch file or script.  All lines have data except for the few at the end.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can remove all blank lines using just

Find /v "" < file.txt > file2.txt

And then process file2.txt or rename as needed.

On phone so will pass more if needed when on PC.

Steve
Avatar of oBdA
oBdA

The Find /v "" < file.txt > file2.txt doesn't work for me; file2 still has the empty lines.
What works is findstr.exe and a regular expression:
findstr.exe /r "." < test.txt > test2.txt

Open in new window

oBdA ... that is bizarre, agreed it doesn't work yet sure I've used it many times before!!
I think you guys already knew this :).  See Q_26947689.

Looks like a reasonable short version is

findstr . file1.txt >file2.txt

~bp
I suppose the other logical way is to select the lines that are wanted too, i.e. selecting all lines with comma or 0 or some other character that MUST be in each line for it to be imported anyway.

Steve
Avatar of Mohonk

ASKER

Worked like a charm.  Thank you!!