batch-file - 使用批处理选择严重性最高的值

标签 batch-file

我有一个以下格式的 CSV 文件:

Group   Test    Result
----------------------
GROUP1  Test1   Amber
GROUP1  Test1   Amber
GROUP1  Test2   Red
Group1  Test3   Amber
Group1  Test4   Green
Group2  Test1   Amber
Group2  Test2   Amber
Group3  Test1   Green
Group3  Test2   Amber
...

使用批处理文件,我试图确定上面列出的每个组的累积结果。对于每组,累积结果是严重程度最高的结果(红色 最严重,琥珀色 不太严重,绿色 最不严重)。遵循这一逻辑人们会期望:

Group1 cumulative result = Red
Group2 cumulative result = Amber
Group3 cumulative result = Amber
...

文件可以有多个这样的组(不仅仅是 3 个)

现在我希望批处理文件输出到包含以下格式的文本文件:

Group1,Group2,Group3
Red,Amber,Amber

有人对我如何做到这一点有建议吗?我认为会涉及 for/f 循环。

最佳答案

@echo off
setlocal EnableDelayedExpansion
set Green=1
set Amber=2
set Red=3
rem Read file lines and process they
for /F "tokens=1-3" %%a in (thefile.csv) do (
   rem Initialize this "group" vector element with zero
   if not defined group[%%a] set group[%%a]=0
   rem Convert "word" severity to a number between 1 and 3
   set severity=!%%c!
   rem Store the cumulative result for this group
   if !severity! gtr !group[%%a]! set group[%%a]=!severity!
)
rem Convert cumulative group numeric results to "word" results
set Severity[1]=Green
set Severity[2]=Amber
set Severity[3]=Red
for /F "tokens=2,3 delims=[]=" %%a in ('set group[') do (
   set group[%%a]=!Severity[%%b]!
)
rem Assemble group names in one line and show they:
set result=
for /F "tokens=2 delims=[]=" %%a in ('set group[') do (
   set result=!result!%%a,
)
echo %result:~0,-1%
rem Assemble group cumulative results in one line and show they:
set result=
for /F "tokens=3 delims=[]=" %%a in ('set group[') do (
   set result=!result!%%a,
)
echo %result:~0,-1%

关于batch-file - 使用批处理选择严重性最高的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9126119/

相关文章:

windows - 如何从批处理例程中输出特定行?

windows - 编译批处理文件全屏运行

excel - 使用批处理文件重命名和格式化 Excel 工作表

html - 在 HTA 中从 JScript 运行批处理文件

windows - 通过批处理 “CMD” 文件识别操作系统、体系结构并执行特定工作

batch-file - 如何在 wsl 中从 Windows 批处理文件执行 .sh 文件

command-line - 有任何方法可以直接将vbscript运行到wscript或cscript命令行

batch-file - 在命令提示符中连接列表

batch-file - 如何使用 Windows 批处理文件搜索值并替换为外部文件中的另一个值

windows - 使用 .bat 脚本复制整个文件夹