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

11,661

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.

Share:
11,661
new
Author by

new

Updated on June 13, 2022

Comments

  • new
    new almost 2 years

    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.

    • rojo
      rojo about 11 years
      What's improper about the job it does?
    • new
      new about 11 years
      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
    new about 11 years
    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!
  • rojo
    rojo about 11 years
    @new - Excellent. I'm glad it worked for you. If you agree that it's appropriate, please consider marking my answer as accepted.