Using FINDSTR to locate whitespace lines

Microsoft Windows xp professional w/serv...
May 27, 2010 at 17:50:26
Specs: Windows XP
I have a file where some lines consist of whitespaces only. I need to skip those lines and only display lines that are not whitespaces.

Say, the file looks like this:

Beginnng of file.
                                                   
                                                   
Middle of file.
                                                   
	                                   
This is next to end line of file.
This is end of file.

I have the following batch script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq delims=" %%a in ("file.txt") DO (
    ECHO.%%a | findstr "^[\t ]*$"
    IF errorlevel=1 ECHO.%%a
)
SETLOCAL DISABLEDELAYEDEXPANSION

I am expecting to see the following result:

Beginning of file.
Middle of file.
This is next to end line of file.
This is end of file.

However it's not working. My regular expression is incorrect. Please help me fix it.

-Greg.


See More: Using FINDSTR to locate whitespace lines

Report •


#1
May 27, 2010 at 22:32:52
findstr sucks. I used to like it, but it is very weak.
this should work:
findstr /r "[^\ ]" test.txt
for what you want, but the command totally bombs.
and a for-example (just test for space at beg. of line)
findstr /r "^\ "
should work. Instead it bitches that there are no strings supplied.
It works for some things, but sucks for others (mostly,
apparently, involving spaces.)
you could work around it:
for /f %%a in (file) do (
set test=%%a
set test=!test: =!
if "!test!" neq "" >> newfile echo %%a
)
::---- end
It's not your fault, this one's squarely in MS's jurisdiction
AFAIC, putting out sucky commands.

Report •

#2
May 28, 2010 at 08:29:43
Nbrane, your alternate solution worked like a charm! Thanks for your help!

Report •

#3
May 28, 2010 at 11:38:23
Actually it works for lines consisting of spaces only. It does not work for lines with tabs. How do I update it for tabs?

Report •

Related Solutions

#4
May 28, 2010 at 11:47:39
I replaced

SET SQL_LINE=!SQL_LINE: =!

with

SET SQL_LINE=!SQL_LINE:	= !
SET SQL_LINE=!SQL_LINE: =!

and it worked.


Report •

#5
May 28, 2010 at 14:47:34
Actually, this very simple batch file will do the job just nicely:

@ECHO OFF
FOR /F "usebackq delims=" %%a in ("file.txt") DO (
ECHO.%%a
)


Report •

#6
May 28, 2010 at 16:05:33
Nope, it won't.

Report •

#7
May 28, 2010 at 17:41:57
Really that's a better way, just needs tweaking:

@ECHO OFF
FOR /F "usebackq tokens=*" %%a in ("file.txt") DO if "%%a" neq "" ECHO.%%a




Report •

#8
May 29, 2010 at 05:15:29
@grishkin: "Nope, it won't"

Yes, it will. Have you tried it?

@nbrane: "just needs tweaking: if "%%a" neq ""

Not necessary. %%a will never be the empty string. FOR /F loops always skip blank lines (including those with only whitespace).


Report •

#9
May 29, 2010 at 05:32:14
@klint, hello: my experience is that null lines ARE skipped, but one or more whitespaces aren't skipped. ?

Report •

#10
May 29, 2010 at 06:51:35
I tried it with lines containing both spaces and tabs (and nothing else). They were all skipped. This was on Vista. Are you seeing different results in XP? I thought CMD.EXE behaved similarly in both versions.

Report •

#11
May 29, 2010 at 11:32:53
w2k doesn't ignore tabs. Here we go agin with version frolics. This does it:

========================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=*" %%a in (myfile) do (
if "%%a" neq "" echo.%%a
)


=====================================
Helping others achieve escape felicity

M2


Report •

#12
May 29, 2010 at 13:21:24
Lol! "version frolicks"! good way of putting it. My test was
XP, using ".bat" and ".cmd". From another post i saw on here (http://www.computing.net/answers/programming/cmd-vsbatch-different-output/21600.html), a guy running win7 commented on a lot of diffs
between .cmd and .bat on his system, but on XP i don't
think there's much difference except the extension itself.
Maybe designed for cross-platform compat, or maybe just
more "version frolicks" by MS et al.

Report •

#13
May 29, 2010 at 13:53:11
I saw that one. It was too confused to make any meaningful comment about.


=====================================
Helping others achieve escape felicity

M2


Report •

#14
May 29, 2010 at 16:09:12
I apologize for the confusion but just realised that in my test I forgot to add "delims=" or "tokens=*". Without either of those, the lines containing whitespace really are skipped (but then you only get the first word of each line). When you add "delims=" then you do get the whitespace lines as everyone noted above. Both on XP and Vista..

Report •

#15
May 29, 2010 at 16:55:46
Hi klint,

I didn't mean your thing. I meant that cmd v bat thread.


=====================================
Helping others achieve escape felicity

M2


Report •

#16
May 30, 2010 at 15:09:46
That's ok M2, I understood you meant the cmd v bat thread, though I didn't see that one so don't know exactly what it was about.

What I meant was that I hadn't tested using the exact same code that I had posted.


Report •

Ask Question