![](/img/trans.png)
[英]How to delete one line before a specific word in a text file in python
[英]Delete text before specific word in text file using batch
这是我正在寻找的一个例子。 假设我们有一个包含以下内容的文本文件:
eggs bacon
delete me here
test me
delete this too here
我想要一个批处理文件,删除“这里”一词之前的所有内容,但只删除同一行中的所有内容。
所以 output 应该是:
eggs bacon
here
test me
here
我尝试使用 for 循环逐个解析文本标记,但它让我无处可去。
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q66181497.txt"
SET "outfile=%destdir%\outfile.txt"
SET "dropbefore=here"
(
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
rem transfer line read to variable "line" for manipulation
SET "line=%%b"
IF "%%b" equ "!line:*%dropbefore%=!" (
rem target text missing from line
ECHO %%b
) ELSE (
rem target found on line. replace all-before-target with target
ECHO !line:*%dropbefore%=%dropbefore%!
)
)
)>"%outfile%"
GOTO :EOF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.