批处理以扫描文件中的字符串并在所有出现的地方获取上面的行,然后>output.txt
batch to scan a file for a string and get the line above in all occurrences, then>output.txt
我有一个 list.txt 文件,就像这个小样本:
banana.zip
xxx / yyy / zzz
orange.zip
kkklllmmm
lemon.zip
abc / def / ghi
apple.zip
xxx / yyy / zzz
pineaple.zip
kkklllmmm
我需要根据以下行过滤 .zip 行。
所以,如果我选择字符串 kkklllmmm,结果需要是:
orange.zip
pineaple.zip
我已经启动了一个批处理脚本,但是它不完整:
@echo off
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
for /f "delims=:" %%# in ('findstr /n /c:"%string%" "%input_list%"') do (
set "line=%%#"
set /a "lineBefore=line-1"
for /f "delims=:" %%a in ("[CONTENT OF %lineBefore%]") do echo %%a>%output_list%)
如果能帮助修复此代码并使其正常工作,我将不胜感激。
谢谢。
解释 Squashman 的建议:
@echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
(for /f "delims=" %%# in (%input_list%) do (
REM if current line is your searchstring, then echo the previous line:
if "%%#"=="%string%" echo !line!
REM set the variable to the current line:
set "line=%%#"
)) > "%output_list%"
注意:我使用空分隔符来读取整行。如果出于某种原因(除了 "some character that isn't in the file to read the whole line")你有意使用 :
,只需重新实现它即可。
编辑:
How did you get the previous line?
通过设置 line
变量 after 比较 %%#
(当前行),所以循环的每一轮都在结束 line
是当前行,但在下一回合开始时,它将是 "previous line"(并且仅在回合结束时变为 "current line")
I need to find more than one string
:有点复杂:添加第二个for
来处理每个搜索字符串。请注意 set "string=..."
行的语法。要同时处理带空格的字符串,此语法是必需的。 (如果没有空格,它会简化为 set "string=string1 string2 string3")
,但要处理空格,每个子字符串也必须被引用:
set "string="string 1" "string 2" "string 3""
)
@echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string="kkklllmmm" "xxx / yyy / zzz""
set "output_list=output.txt"
(for /f "delims=:" %%# in (%input_list%) do (
for %%a in (%string%) do (
echo %%#|findstr /xc:%%a >nul && echo !line!
)
set "line=%%#"
)) > "%output_list%"
外层for
(%%#
)一次一行。
内部 for
一次处理列表中的每个子字符串。
下一行 echo
es 该行并尝试查找子字符串(开关
x
=比较完整的行,
c
=搜索文字字符串(包括空格,否则它会尝试查找字符串中的每个单词,例如 xxx
、/
、yyy
等)
&&
仅执行命令 echo !line!
,前提是前一个命令成功(findstr
找到匹配项)
您使用了 PowerShell 命令:
powershell -nop -c "sls List.txt -Patt 'kkklllmmm' -Co 1,0|%{$_.Context.PreContext}">output.txt
或者打包成一个批次:
@echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
(powershell -nop -c "sls '%input_List%' -Patt '%string%' -Co 1,0|%%{$_.Context.PreContext}"
)>"%output_list%"
示例输出
orange.zip
pineaple.zip
我有一个 list.txt 文件,就像这个小样本:
banana.zip
xxx / yyy / zzz
orange.zip
kkklllmmm
lemon.zip
abc / def / ghi
apple.zip
xxx / yyy / zzz
pineaple.zip
kkklllmmm
我需要根据以下行过滤 .zip 行。 所以,如果我选择字符串 kkklllmmm,结果需要是:
orange.zip
pineaple.zip
我已经启动了一个批处理脚本,但是它不完整:
@echo off
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
for /f "delims=:" %%# in ('findstr /n /c:"%string%" "%input_list%"') do (
set "line=%%#"
set /a "lineBefore=line-1"
for /f "delims=:" %%a in ("[CONTENT OF %lineBefore%]") do echo %%a>%output_list%)
如果能帮助修复此代码并使其正常工作,我将不胜感激。
谢谢。
解释 Squashman 的建议:
@echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
(for /f "delims=" %%# in (%input_list%) do (
REM if current line is your searchstring, then echo the previous line:
if "%%#"=="%string%" echo !line!
REM set the variable to the current line:
set "line=%%#"
)) > "%output_list%"
注意:我使用空分隔符来读取整行。如果出于某种原因(除了 "some character that isn't in the file to read the whole line")你有意使用 :
,只需重新实现它即可。
编辑:
How did you get the previous line?
通过设置 line
变量 after 比较 %%#
(当前行),所以循环的每一轮都在结束 line
是当前行,但在下一回合开始时,它将是 "previous line"(并且仅在回合结束时变为 "current line")
I need to find more than one string
:有点复杂:添加第二个for
来处理每个搜索字符串。请注意 set "string=..."
行的语法。要同时处理带空格的字符串,此语法是必需的。 (如果没有空格,它会简化为 set "string=string1 string2 string3")
,但要处理空格,每个子字符串也必须被引用:
set "string="string 1" "string 2" "string 3""
)
@echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string="kkklllmmm" "xxx / yyy / zzz""
set "output_list=output.txt"
(for /f "delims=:" %%# in (%input_list%) do (
for %%a in (%string%) do (
echo %%#|findstr /xc:%%a >nul && echo !line!
)
set "line=%%#"
)) > "%output_list%"
外层for
(%%#
)一次一行。
内部 for
一次处理列表中的每个子字符串。
下一行 echo
es 该行并尝试查找子字符串(开关
x
=比较完整的行,
c
=搜索文字字符串(包括空格,否则它会尝试查找字符串中的每个单词,例如 xxx
、/
、yyy
等)
&&
仅执行命令 echo !line!
,前提是前一个命令成功(findstr
找到匹配项)
您使用了 PowerShell 命令:
powershell -nop -c "sls List.txt -Patt 'kkklllmmm' -Co 1,0|%{$_.Context.PreContext}">output.txt
或者打包成一个批次:
@echo off
setlocal enabledelayedexpansion
set "input_list=list.txt"
set "string=kkklllmmm"
set "output_list=output.txt"
(powershell -nop -c "sls '%input_List%' -Patt '%string%' -Co 1,0|%%{$_.Context.PreContext}"
)>"%output_list%"
示例输出
orange.zip
pineaple.zip