使用 findstr 删除文本行
Delete textline with findstr
我已经通过下面的代码移动了以特定文本开头的行的文件。
在此之前是可能的,但我想删除除了只有特定文本的行之外的文本,
但是我不知道该怎么办。
for /f %%a in ('findstr /b /m "a" *.txt') do (
move /y "%%~a" "D:change_text"
)
下面是文本示例。
ex1)
b 0.d4g7a6 0.4g1h5s 0.b9g5r2 0.6s7d2f
a 0.6d7g1a 0.6g1g8a 0.6z4s6f 0.g7w2a7x
到
a 0.6d7g1a 0.6g1g8a 0.6z4s6f 0.g7w2a7x
ex2)
d 0.5g98 0.6b3n8 0.3s4q2a 0.s3z6d9f
a 1.6g2 0.5c9d4 0.1a7ge2z 0.1fe4sz6x
a 0.3q8t6e 0.5q8r6q 2.1a4zx9vs 0.1q2s6c
z 0.6p2o3t 0.e9 0.1q8s6z 0.v0s9d4f
a 0.7i6i1l 0.6u9q4 0.0c2v9 0.0z5s5d
a 0.9q3z 0.6s7d6f 0.0w3s9f 0.h0y1y8u
到
a 1.6g2 0.5c9d4 0.1a7ge2z 0.1fe4sz6x
a 0.3q8t6e 0.5q8r6q 2.1a4zx9vs 0.1q2s6c
a 0.7i6i1l 0.6u9q4 0.0c2v9 0.0z5s5d
a 0.9q3z 0.6s7d6f 0.0w3s9f 0.h0y1y8u
说说意见
可以用其他语言告诉。
好吧,findstr
只查找文本,但不会从文件中删除文本。您正在搜索包含特定文本的文件,然后将它们移动到某个地方,因此您错过了实际删除文本的步骤。你可以这样做:
rem /* Search files that contain matching text and loop through them;
rem since the `/M` option is specified the file names are returned;
rem the search string `^a\>` searches the word `a` at the beginning
rem of the line (`^`), `\>` constitutes a word boundary: */
for /F "delims= eol=|" %%F in ('findstr /M "^a\>" "*.txt"') do (
rem /* Search the current file again but return the matching text this time;
rem write that text to another file using redirection (`>`);
rem if successful (`&&`), delete the current (original) file: */
(> "D:\%%~F" findstr "^a\>" "%%~F") && del "%%~F"
)
我已经通过下面的代码移动了以特定文本开头的行的文件。
在此之前是可能的,但我想删除除了只有特定文本的行之外的文本,
但是我不知道该怎么办。
for /f %%a in ('findstr /b /m "a" *.txt') do (
move /y "%%~a" "D:change_text"
)
下面是文本示例。
ex1)
b 0.d4g7a6 0.4g1h5s 0.b9g5r2 0.6s7d2f
a 0.6d7g1a 0.6g1g8a 0.6z4s6f 0.g7w2a7x
到
a 0.6d7g1a 0.6g1g8a 0.6z4s6f 0.g7w2a7x
ex2)
d 0.5g98 0.6b3n8 0.3s4q2a 0.s3z6d9f
a 1.6g2 0.5c9d4 0.1a7ge2z 0.1fe4sz6x
a 0.3q8t6e 0.5q8r6q 2.1a4zx9vs 0.1q2s6c
z 0.6p2o3t 0.e9 0.1q8s6z 0.v0s9d4f
a 0.7i6i1l 0.6u9q4 0.0c2v9 0.0z5s5d
a 0.9q3z 0.6s7d6f 0.0w3s9f 0.h0y1y8u
到
a 1.6g2 0.5c9d4 0.1a7ge2z 0.1fe4sz6x
a 0.3q8t6e 0.5q8r6q 2.1a4zx9vs 0.1q2s6c
a 0.7i6i1l 0.6u9q4 0.0c2v9 0.0z5s5d
a 0.9q3z 0.6s7d6f 0.0w3s9f 0.h0y1y8u
说说意见
可以用其他语言告诉。
好吧,findstr
只查找文本,但不会从文件中删除文本。您正在搜索包含特定文本的文件,然后将它们移动到某个地方,因此您错过了实际删除文本的步骤。你可以这样做:
rem /* Search files that contain matching text and loop through them;
rem since the `/M` option is specified the file names are returned;
rem the search string `^a\>` searches the word `a` at the beginning
rem of the line (`^`), `\>` constitutes a word boundary: */
for /F "delims= eol=|" %%F in ('findstr /M "^a\>" "*.txt"') do (
rem /* Search the current file again but return the matching text this time;
rem write that text to another file using redirection (`>`);
rem if successful (`&&`), delete the current (original) file: */
(> "D:\%%~F" findstr "^a\>" "%%~F") && del "%%~F"
)