Computing.Net > Forums > Programming > delete lines with only spaces

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

delete lines with only spaces

Reply to Message Icon

Name: neilblue
Date: July 14, 2008 at 05:43:57 Pacific
OS: XP
CPU/Ram: ?
Product: DELL
Comment:

Greetings I'm looking to delete lines that have only spaces and no other characters.

I had this script to remove blank lines but it has issues with lines that have one or two spaces in it.

SetLocal EnableDelayedExpansion
For %%J in (test.tmp) Do (
For /F "tokens=* delims=" %%A in (%%J) Do (
Set Row=%%A
If not "!Row:~0,1!"=="#" (
:LOOP
If "!Row:~-1,1!"==" " (
(Set Row=!Row:~0,-1!) & GoTo :LOOP)
Set Row=!Row:tiplespawn 0=tiplespawn 1!
Echo !Row! >> %%~nJ_new.tmp)))

Is there a way to fix this one or a new one will be fine. Thank you



Sponsored Link
Ads by Google

Response Number 1
Name: devil_himself
Date: July 14, 2008 at 08:20:01 Pacific
Reply:

findstr /v "^$" MyFile.txt > NoBlank.txt


0

Response Number 2
Name: neilblue
Date: July 14, 2008 at 10:37:20 Pacific
Reply:

This did not seem to do anything.


0

Response Number 3
Name: klint
Date: July 14, 2008 at 14:08:25 Pacific
Reply:

If you are referring to the following lines in your originally posted code:

:LOOP
If "!Row:~-1,1!"==" " (
(Set Row=!Row:~0,-1!) & GoTo :LOOP)

This appears to be an attempt to trim spaces off the end of the line. Unfortunately, the command processor doesn't like labels inside for loops. Try sticking the above in a subroutine and calling it from your for loop.


0

Response Number 4
Name: dtech10
Date: July 14, 2008 at 15:20:34 Pacific
Reply:

Hi

This help

@echo off
type nul > NewFile.txt
for /f "tokens=*" %%a in ('type Blanks.txt') do (
if not "%%a"=="" echo %%a >> Newfile.txt
)


0

Response Number 5
Name: klint
Date: July 15, 2008 at 02:30:42 Pacific
Reply:

To dtech10:

for /f "tokens=*" %%a in ('type Blanks.txt') do (
if not "%%a"=="" echo %%a >> Newfile.txt
)

That's unnecessary, because the for command will automatically skip all blank lines. You will never get the condition "%%a"=="" that you're testing above.

I think the original poster wants to remove blanks from existing non-blank lines, as shown by his code.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: July 15, 2008 at 03:45:57 Pacific
Reply:

@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%b in (my.txt) do (
if not "%%b"=="" echo %%b >> newfile
)


0

Response Number 7
Name: klint
Date: July 15, 2008 at 04:11:43 Pacific
Reply:

@M2: that's the same as dtech10's post!


0

Response Number 8
Name: Mechanix2Go
Date: July 15, 2008 at 04:20:51 Pacific
Reply:

Hi klint,

Amazing the way great minds think alike.

::===============================
"That's unnecessary, because the for command will automatically skip all blank lines. You will never get the condition "%%a"=="" that you're testing above."

If you leave out that conditional, your newfile will contain"

echo is off


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: neilblue
Date: July 15, 2008 at 04:36:15 Pacific
Reply:

I guess it's hard to explain. I'm starting with a file that are seperated by blank lines.

line1

line2

line3

Most of the time the code that I have works nicely to remove the blank lines to make it look like this

line1
line2
line2

but I had a situation where in one of the blank lines there was a space character (if that makes sense)

line1

line2

line3

Being in between line2 and line3 there is a space. So the code that I posted originally deletes the blank line between line1 and line2 but then errors when it hits the space character between line2 and line3 making the output file look like this

line1
line2

and basically strips away the third line or anything below it (if i had 100 lines after it for example).

I hope that's a bit more clean


0

Response Number 10
Name: klint
Date: July 15, 2008 at 05:47:06 Pacific
Reply:

M2, and dtech10, I see how your code works now. The magic is in the fact that spaces are treated as delimiters. It causes leading spaces to be stripped off each line. So, if a line has nothing but spaces, it becomes an empty string. So you're right, you do need the conditional. I misunderstood the purpose, and thought you were trying to remove completely blank (empty) lines, which are automatically skipped.

So, neilblue, please try dtech10's or M2's suggestion, they both got it right.


0

Response Number 11
Name: neilblue
Date: July 15, 2008 at 06:24:02 Pacific
Reply:

dtech10's code works, although it adds a space at the end of each line as well. not sure why it would do this though.

M2's script just removes everything and the new file is blank.


This code will work just fine.
Thank you all for your help.


0

Response Number 12
Name: klint
Date: July 15, 2008 at 06:53:45 Pacific
Reply:

neilblue, the space is added because of the echo command:

if not "%%a"=="" echo %%a >> Newfile.txt

Just change it to

if not "%%a"=="" echo %%a>> Newfile.txt


0

Response Number 13
Name: neilblue
Date: July 15, 2008 at 08:04:55 Pacific
Reply:

Ah.. thank you klint

It actually didn't cause an issue when I tested it out. but it's good reference for future knowledge.

Thank you.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: delete lines with only spaces

Deleting lines with batch files www.computing.net/answers/programming/deleting-lines-with-batch-files/13330.html

del blank lines, remove white space www.computing.net/answers/programming/del-blank-lines-remove-white-space/15304.html

Batch File To Delete Line www.computing.net/answers/programming/batch-file-to-delete-line/15539.html