Page 1 of 1

Findstr for blank lines

Posted: 23 Jun 2012 16:09
by MrKnowItAllxx
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

Re: Findstr for blank lines

Posted: 23 Jun 2012 17:18
by Ed Dyreen
'
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. . .

Re: Findstr for blank lines

Posted: 23 Jun 2012 18:12
by MrKnowItAllxx
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

Re: Findstr for blank lines

Posted: 23 Jun 2012 18:29
by Ed Dyreen
'
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> ?

Re: Findstr for blank lines

Posted: 23 Jun 2012 20:17
by dbenham
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

Re: Findstr for blank lines

Posted: 23 Jun 2012 23:23
by MrKnowItAllxx
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