简体   繁体   English

Windows批处理文件-多个条件和行

[英]Windows Batch File - Multiple Conditions and Lines

I need to extract information from a file using BATCH file. 我需要使用BATCH文件从文件中提取信息。 I have AWK (GNUWIN32) installed in Windows. 我在Windows中安装了AWK(GNUWIN32)。

Text File contains - 文本文件包含-

# FOR Example Purposes  *** #
# Dated on Jan 29, 2014

TEST_tag=branch_release_v_2.0.82.91_YOU_Branch
NONE_tag=branch_release_v_12.0.82.1_ONE_Branch
DOIT_tag=branch_release_v_1.0.9_EXA_Branch

Now, I need the following output from this file (comments removed and string manipulated)- 现在,我需要此文件的以下输出(删除注释并操作字符串)-

TEST/branch_release_v_2.0.82.91_YOU_Branch
NONE/branch_release_v_12.0.82.1_ONE_Branch
DOIT/branch_release_v_1.0.9_EXA_Branch

I will prefer if it can be done using variables instead of replacing "_tag=" with "/" so, I can use the logic with other formats as well. 我更愿意使用变量而不是用“ /”代替“ _tag =“”,因此,我也可以将逻辑与其他格式一起使用。

Here what I tried to do. 这是我尝试做的。

FOR /F "tokens=*" %%A IN ('awk "!/^($|#)/" test.txt') DO (
ECHO %%A | awk -F_ "{print $1}" > one.tmp
SET /P ONE=<one.tmp

ECHO %%A | awk -F= "{print $2}" > two.tmp
SET /P TWO=<two.tmp

ECHO %ONE%/%TWO%
)

It's not working and is printing the same result (last line only printed 3 times) 它不起作用,正在打印相同的结果(最后一行仅打印了3次)

DOIT/branch_release_v_1.0.9_EXA_Branch
DOIT/branch_release_v_1.0.9_EXA_Branch
DOIT/branch_release_v_1.0.9_EXA_Branch

Solution (Thanks to Magoo) --- 解决方案(感谢Magoo)---

FOR /F "tokens=*" %%A IN ('awk "!/^($|#)/" test.txt') DO (

setlocal enabledelayedexpansion

ECHO %%A | awk -F_ "{print $1}" > one.tmp
SET /P ONE=<one.tmp

ECHO %%A | awk -F= "{print $2}" > two.tmp
SET /P TWO=<two.tmp

ECHO !ONE!/!TWO!
)

Thanks again. 再次感谢。

Within a block statement (a parenthesised series of statements) , the entire block is parsed and then executed. 在块语句(a parenthesised series of statements) ,将分析整个然后执行。 Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed. 解析该块时-执行该块之前,该块中的任何%var%都将由该变量的值替换。

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered. 因此,将在遇到IF时使用%variables%的值执行IF (something) else (somethingelse)

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! 解决此问题的两种常用方法是:1)使用setlocal enabledelayedexpansion和使用!var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values. 代替%var%访问的改变值var或2),以调用一个子程序使用改变的值来执行进一步的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM