Findstr for blank lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Findstr for blank lines

#1 Post by MrKnowItAllxx » 23 Jun 2012 16:09

I'm simply trying to find a findstr command expression that will echo back all blank lines of the file with their line number (including lines that consist of all nonvisible characters)

So far I have:

Code: Select all

findstr /n /v /r "^." "file.txt"


But this will not echo back a line that has spaces in it, but no visible characters

Any help appreciated

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Findstr for blank lines

#2 Post by Ed Dyreen » 23 Jun 2012 17:18

'
That will echo back all blank lines of the file with their line number, use regular expressions for the nonvisible.

Code: Select all

@echo off &setlocal enableDelayedExpansion

> "out.txt" (

   echo.line1
   echo.
   echo.line3
)

for /f "tokens=1-2 delims=[]" %%a in (

   '2^>nul ^( ^< "out.txt" find /n /v "." ^)'

) do    echo.%%b|>nul findstr /v "." &&(

   echo.
   echo.$c: '%%a'
   echo.$$: '%%b'
)

pause
exit

Code: Select all


$c: '2'
$$: ''
Druk op een toets om door te gaan. . .

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Findstr for blank lines

#3 Post by MrKnowItAllxx » 23 Jun 2012 18:12

use regular expressions for the nonvisible

How would you do that?

I have tried things like

Code: Select all

findstr /n /r "^[\ ]*" file.txt


but I can't seem to get them to work

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Findstr for blank lines

#4 Post by Ed Dyreen » 23 Jun 2012 18:29

'
As dBenham explained, be careful with special chars need special handling !
viewtopic.php?p=17275#p17275

Which non-visible variables are u talking about <space>,<tab> ?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Findstr for blank lines

#5 Post by dbenham » 23 Jun 2012 20:17

My first thought is to search for strings that contain 0 or more tabs or spaces with nothing else. Note that my character set is supposed to consist of space and tab, but I can't enter tab into this forum, so I put 2 spaces.

Code: Select all

findstr /n /r /c:"^[  ]*$" test.txt
But the above will not work if the last line is blank and does not end with a line feed character. The $ metacharacter only matches lines that end with a line feed.

The trick is to look for lines that do not match a regex that matches any character other than space or tab. The character set is a "not" set containing a space and a tab (again, I can't enter the tab here).

Code: Select all

findstr /n /r /v /c:"[^  ]" test.txt


Dave Benham

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: Findstr for blank lines

#6 Post by MrKnowItAllxx » 23 Jun 2012 23:23

findstr /n /r /v /c:"[^ ]" test.txt


Works perfectly! I need to keep working on this findstr stuff, idk why it seems to confuse me so :?

Thanks for the help guys

Post Reply