Findstr 结果操作和循环批处理文件
Findstr Results Manipulation & Loop Batch File
我正在编写一个脚本,该脚本从 txt 行中的文件中收集值,我通过使用 findstr 并引用关键字找到这些值。我要完成的过程是这样的:
- 如果该行包含关键字“word1”,则在文件中查找VALUE1并显示VALUE1。
- 如果该行包含关键字“word1”,则在文件中查找 VALUE2。
- 如果该行包含关键字“word2”,则在文件中查找 VALUE3(我也知道这个值将在单独的一行上)。
- 将 VALUE2 和 VALUE3 相加并将此值显示为总计。
- 通过步骤 1-4 再次遍历文件,如果存在另一组 VALUE1、VALUE2 和 VALUE3,则显示另一个 TOTAL。
示例文件(test.txt):
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05032017 and you have 5 apples
Also it is good to know that you bought 6 peaches
关键字:“苹果”、“桃子”
期望的输出:
First value is: 05022017
Second value is: 4
Third value is: 5
TOTAL: 9
First value is: 05032017
Second value is: 5
Third value is: 6
TOTAL: 11
当前代码:
@echo off
FOR /f "tokens=4,8" %%a in ('type test.txt ^|findstr /i "apples"') do (
echo(First value is: %%a
echo(Second value is: %%b
)
FOR /f "tokens=10" %%c in ('type test.txt ^|findstr /i "peaches"') do (
echo(Value on another line is: %%c
)
echo(All done!
echo(&pause&goto:eof
当前输出:
First value is: 05022017
Second value is: 4
First value is: 05032017
Second value is: 5
Value on another line is: 5
Value on another line is: 6
我明白为什么我的代码会这样工作。我知道我有两个单独的循环,首先找到第一个和第二个值,然后找到位于不同行上的第三个值。我尝试过使用向量、嵌套循环和为变量赋值,但我似乎无法得到我想要的工作。
我希望能够 运行 在具有 N 种可能性的文件上使用此脚本,这意味着它应该适用于这两种文件情况:
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
或
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
我将不胜感激任何我能获得的帮助或一些关于如何以不同方式处理此问题的指导。
谢谢!
更新工作代码:
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=I:\Tests"
SET "filename1=%sourcedir%\test.txt"
SET "word1=apples"
SET "word2=peaches"
FOR /f "tokens=4,8,9,10,11 delims= " %%a IN ('findstr "%word1% %word2%" "%filename1%"') DO (
IF /i "%%~c"=="%word1%" (
REM apple line
ECHO First value is: %%a
ECHO Second value is: %%b
SET /a value2=%%b
)
IF /i "%%~e"=="%word2%" (
REM peaches line
ECHO Third value is: %%d
SET /a total=value2 + %%d
ECHO Total: !total!
)
)
GOTO :EOF
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q44875889.txt"
SET "word1=%~1"
SET "word2=%~2"
FOR /f "tokens=4,8,9,10,11delims= " %%a IN ('findstr /Li /c:"%word1%" /c:"%word2%" "%filename1%"') DO (
IF /i "%%~c"=="%word1%" (
REM apple line
ECHO First value is: %%a
ECHO Second value is: %%b
SET /a value2=%%b
)
IF /i "%%~e"=="%word2%" (
REM peaches line
ECHO Third value is: %%d
SET /a total=value2 + %%d
ECHO Total: !total!
)
)
GOTO :EOF
您需要更改 sourcedir
的设置以适合您的情况。
我使用了一个名为 q44875889.txt
的文件,其中包含您的数据用于我的测试。
最好能为我们提供真实的数据。
处理应该很明显 - 读取 findstr
的每一行输出,它正在寻找任何一个单词并选择适当的标记。
如果第一个词出现在要求的位置,则处理并保存要求的value2
。同上第二个字,计算总数并使用delayed expansion
显示total
的运行时间值
尝试处理不符合要求格式的行显然没有意义。
重要(但原省略)运行宁须知:
运行 作为
这个批次名称苹果桃子
我原来的帖子中的 word1/2 然后用提供的两个词代替。
(由于没有提供任何参数,word1/2 为空,因此出现错误消息)
我正在编写一个脚本,该脚本从 txt 行中的文件中收集值,我通过使用 findstr 并引用关键字找到这些值。我要完成的过程是这样的:
- 如果该行包含关键字“word1”,则在文件中查找VALUE1并显示VALUE1。
- 如果该行包含关键字“word1”,则在文件中查找 VALUE2。
- 如果该行包含关键字“word2”,则在文件中查找 VALUE3(我也知道这个值将在单独的一行上)。
- 将 VALUE2 和 VALUE3 相加并将此值显示为总计。
- 通过步骤 1-4 再次遍历文件,如果存在另一组 VALUE1、VALUE2 和 VALUE3,则显示另一个 TOTAL。
示例文件(test.txt):
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05032017 and you have 5 apples
Also it is good to know that you bought 6 peaches
关键字:“苹果”、“桃子”
期望的输出:
First value is: 05022017
Second value is: 4
Third value is: 5
TOTAL: 9
First value is: 05032017
Second value is: 5
Third value is: 6
TOTAL: 11
当前代码:
@echo off
FOR /f "tokens=4,8" %%a in ('type test.txt ^|findstr /i "apples"') do (
echo(First value is: %%a
echo(Second value is: %%b
)
FOR /f "tokens=10" %%c in ('type test.txt ^|findstr /i "peaches"') do (
echo(Value on another line is: %%c
)
echo(All done!
echo(&pause&goto:eof
当前输出:
First value is: 05022017
Second value is: 4
First value is: 05032017
Second value is: 5
Value on another line is: 5
Value on another line is: 6
我明白为什么我的代码会这样工作。我知道我有两个单独的循环,首先找到第一个和第二个值,然后找到位于不同行上的第三个值。我尝试过使用向量、嵌套循环和为变量赋值,但我似乎无法得到我想要的工作。
我希望能够 运行 在具有 N 种可能性的文件上使用此脚本,这意味着它应该适用于这两种文件情况:
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
或
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
Hello today is 05022017 and you have 4 apples
Also it is good to know that you bought 5 peaches
我将不胜感激任何我能获得的帮助或一些关于如何以不同方式处理此问题的指导。
谢谢!
更新工作代码:
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=I:\Tests"
SET "filename1=%sourcedir%\test.txt"
SET "word1=apples"
SET "word2=peaches"
FOR /f "tokens=4,8,9,10,11 delims= " %%a IN ('findstr "%word1% %word2%" "%filename1%"') DO (
IF /i "%%~c"=="%word1%" (
REM apple line
ECHO First value is: %%a
ECHO Second value is: %%b
SET /a value2=%%b
)
IF /i "%%~e"=="%word2%" (
REM peaches line
ECHO Third value is: %%d
SET /a total=value2 + %%d
ECHO Total: !total!
)
)
GOTO :EOF
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q44875889.txt"
SET "word1=%~1"
SET "word2=%~2"
FOR /f "tokens=4,8,9,10,11delims= " %%a IN ('findstr /Li /c:"%word1%" /c:"%word2%" "%filename1%"') DO (
IF /i "%%~c"=="%word1%" (
REM apple line
ECHO First value is: %%a
ECHO Second value is: %%b
SET /a value2=%%b
)
IF /i "%%~e"=="%word2%" (
REM peaches line
ECHO Third value is: %%d
SET /a total=value2 + %%d
ECHO Total: !total!
)
)
GOTO :EOF
您需要更改 sourcedir
的设置以适合您的情况。
我使用了一个名为 q44875889.txt
的文件,其中包含您的数据用于我的测试。
最好能为我们提供真实的数据。
处理应该很明显 - 读取 findstr
的每一行输出,它正在寻找任何一个单词并选择适当的标记。
如果第一个词出现在要求的位置,则处理并保存要求的value2
。同上第二个字,计算总数并使用delayed expansion
显示total
尝试处理不符合要求格式的行显然没有意义。
重要(但原省略)运行宁须知:
运行 作为
这个批次名称苹果桃子
我原来的帖子中的 word1/2 然后用提供的两个词代替。
(由于没有提供任何参数,word1/2 为空,因此出现错误消息)