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

巧了我就是萌 提交于 2021-02-08 05:46:16

问题


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.


回答1:


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.




回答2:


@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



来源:https://stackoverflow.com/questions/16059838/remove-lines-from-a-file-starting-with-a-space-including-empty-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!