batch-file - FOR/F 这是在批处理文件中跳过行的正确方法吗

标签 batch-file variables find

我正在尝试查找并使用特定单词作为变量,以使用批处理脚本打印到 txt 文件中。

这是文本示例。

10-03-2021 18:46:12:77 INFO: [SET_APP_VARS] Country:UK
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Langauge:ENGLISH
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Type:CLASSIC
10-03-2021 18:46:12:78 INFO: [SET_APP_VARS] Lane:801

所以我需要找到单词CLASSIC并将其用作变量

下面的代码正确吗?

FOR /F "skip=2 tokens=1,2,3,4,5,6 delims=:" %%a IN (test.txt) do echo  Country is "%%f" & goto next

:next

FOR /F "skip=4 tokens=1,2,3,4,5,6 delims=:" %%a IN (test.txt) do echo  Motherboard is is "%%f" & goto done

:done

最佳答案

过滤您的输入有助于:

for /f "tokens=6 delims=:" %%a in ('find " Country:" test.txt') do echo Country is %%a
for /f "tokens=6 delims=:" %%a in ('find " Type:" test.txt') do echo Type is %%a

或者如果您需要更多:

    for /f "tokens=8,9 delims=: " %%a in (test.txt) do echo %%a is %%b

或使用变量:

for /f "tokens=8,9 delims=: " %%a in (test.txt) do set "_%%a=%%b"
set _

或只是一些(比第一个片段快得多,因为它只需读取 test.txt 一次):

for /f "tokens=8,9 delims=: " %%a in ('findstr "Country: Type:" test.txt') do set "_%%a=%%b"
set _

关于batch-file - FOR/F 这是在批处理文件中跳过行的正确方法吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66582307/

相关文章:

batch-file - ffmpeg 目录带有空格和 '

linux - Linux 2.4 中的 readlink -f 替代方案

objective-c - 语义问题 : Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects

linux - 强制查找查找所有子目录

regex - 根据名称选择文件

windows - 批处理脚本 : What's the difference between %0 and %~f0?

batch-file - 批量添加多个号码

java - 是否可以使用 JAVA 中保存变量名称的字符串来访问变量?

ruby - ActiveRecord::DangerousAttributeError:更新由 Active Record 定义

linux - 使用 Linux 命令查找并替换