guys, how do you remove an empty lines in a text file using dos batch?
I tried for loop then delimit the space, but i ended up with "echo on message" on the last part of my text file.
thanks in advance.
Quick question
Moderator: DosItHelp
Re: Quick question
Well this is different - usually people want to know how to preserve blank lines.
Based on your description, you don't care about preserving leading spaces on your non-blank lines, and you are relying on FOR /F behavior of ignoring blank lines. You set delims = to a <space> in an attempt to convert a line containing only spaces into a blank line so that it is ignored.
I'm assuming your code looks something like this:
I would have guessed the code would work, but I just learned today that FOR /F ignores blank lines PRIOR to any delimiter parsing. So your lines containing nothing but space are preserved, but converted to a blank line
The code is easily fixed by testing to see if %%L is empty:
I've also added eol=<space> so that lines beginning with ; are not skipped.
If your text file also has <tab> characters and you want to ignore lines containing nothing but <tab> or <space>, then you can simply use delims=<tab><space> instead of delims=<space>
Dave Benham
Based on your description, you don't care about preserving leading spaces on your non-blank lines, and you are relying on FOR /F behavior of ignoring blank lines. You set delims = to a <space> in an attempt to convert a line containing only spaces into a blank line so that it is ignored.
I'm assuming your code looks something like this:
Code: Select all
for /f "%tokens=* delims= " %%L in (file.txt) do echo %%L
I would have guessed the code would work, but I just learned today that FOR /F ignores blank lines PRIOR to any delimiter parsing. So your lines containing nothing but space are preserved, but converted to a blank line
The code is easily fixed by testing to see if %%L is empty:
Code: Select all
for /f "eol= tokens=* delims= " %%L in (file.txt) do if "%%L" neq "" echo %%L
I've also added eol=<space> so that lines beginning with ; are not skipped.
If your text file also has <tab> characters and you want to ignore lines containing nothing but <tab> or <space>, then you can simply use delims=<tab><space> instead of delims=<space>
Dave Benham
Re: Quick question
thanks for the reply Dave, but what if my text file has a line that have spaces in the start of the line that I want to preserve?
Re: Quick question
Then the solution is completely different.
you need to disable DELIMS and EOL (there are a few recent posts that show how to do this) I use my ForEntireLine macro.
then within the body of the FOR DO section:
- set "LN=%%a"
- setlocal enableDelayedExpansion
- set "TEST=!LN: =!" (eliminate all spaces)
- if defined TEST then echo !LN!
- endlocal
Dave Benham
you need to disable DELIMS and EOL (there are a few recent posts that show how to do this) I use my ForEntireLine macro.
then within the body of the FOR DO section:
- set "LN=%%a"
- setlocal enableDelayedExpansion
- set "TEST=!LN: =!" (eliminate all spaces)
- if defined TEST then echo !LN!
- endlocal
Dave Benham
Re: Quick question
thanks Dave for enlightening me. Thanks a lot.
-
- Posts: 126
- Joined: 10 Jun 2011 10:30
Re: Quick question
you need to disable DELIMS and EOL (there are a few recent posts that show how to do this) I use my ForEntireLine macro.
So THAT is what that's for! You would think that with a name like "ForEntireLine" it would be obvious but sometimes you have to be beaten over the head with obvious before you see it!
I seriously thought that "ForEntireLine" was just a macro you used for the *entire FOR options line* with all of the line feeds and escapes necessary to make some of your macros work.
Now I have to go back and re-read all of that so it makes more sense now!
Someone needs to compile a batch rules book. I'm not going to suggest any names, but their initials are Dave and Jeb.
You can find little tips for using the parser's quirks here and there, usually with no explaination attached, just that it works. There are functions you can find everywhere, but one of the most complete sets is right here, but still even when those functions do use little tricks to make them work, it's not really explained.
The only place I've found yet that explains in any detail how and why things work and not just "this is what will do what you want to do, take it and run" is right here.
Re: Quick question
renzlo wrote:thanks for the reply Dave, but what if my text file has a line that have spaces in the start of the line that I want to preserve?
I think you are doing this all wrong.
You should use the "Code" button and use that to enclose the specific things you call "empty lines"
There are many types of empty lines. One man's empty line is another man's poison !
Code: Select all
When I select the entire code box above, I can see one long blue line with lots of "space" characters
and above and below there are lines with no characters.
Regards
Alan