2

To delete all lines of a txt file that begins with a space (including empty lines), I write

    findstr /v /b /c:" " <%1>result.out.

Indeed, result.out file get answer for me if there is no longer both space at beginning of every line and empty line.

What I have done still leaving lines header empty, it also preserves blank lines what i want to give up. Finally, the result.out output must have consecutive lines always containing a text at begining of each line.

Please someone could tell me what it is faulty and how to fix that? Thanks.

new
  • 1,109
  • 5
  • 21
  • 30
  • What's improper about the job it does? – rojo Apr 17 '13 at 12:43
  • What I have done still leaving lines header empty, it also preserves blank lines what i want to give up. Finally, the result.out output must have consecutive lines always containing a text at begining of each line. – new Apr 17 '13 at 13:31

2 Answers2

4

If you want to include Tab characters as part of the whitespace you want to check, you have to use a batch script. The cmd console simply makes an annoyed sound at you if you try to Tab or paste a Tab character into the console. But cmd interprets Tab in a .bat file no problem.

Put this into a batch file and run it, replacing Space and Tab with an actual space and tab.

findstr /r /v /c:"^[SpaceTab]" /c:"^$" "%~1" >result.out

The first /c: checks for whitespace at the beginning of a line. The second /c: checks for blank lines. Both are omitted with the /v switch.

rojo
  • 24,000
  • 5
  • 55
  • 101
  • You're good! so perfect Thank you very much for all. I am new in batch but through the "Stackoverflow" forum, I really understand quite a lot of stuff;) Thank you! – new Apr 17 '13 at 14:09
  • @new - Excellent. I'm glad it worked for you. If you agree that it's appropriate, please consider [marking my answer as accepted](http://meta.stackexchange.com/a/5235/187716). – rojo Apr 17 '13 at 14:31
0
@ECHO OFF
SETLOCAL
CALL CMD /c EXIT 26
(
FOR /f "eol=%=ExitCodeAscii% delims=" %%i IN ('findstr /v /b /c:" " ^<%1') DO ECHO %%i
)>result.out

Should remove blank lines.

The FOR reads each line of the file that appears after FINDSTR has removed all of the lines with trailing spaces. Any empty lies are discarded, and the entire line is applied to %%i because there are no delimiters.

By setting the end-of-line character to control-Z, there are effectively no end-of-lines either, so all non-empty lines are ECHOed

Magoo
  • 77,302
  • 8
  • 62
  • 84