Search code examples
batch-filespacelines

Remove lines from a file starting with a space, including empty lines


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.


Solution

  • 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.