【发布时间】:2018-02-27 17:14:41
【问题描述】:
我对参数替换感到困惑。基本上要解析的文件结构如下:
foo.txt:
system.switch_cpus.commit.op_class_0::total 10000000 # Class of committed instruction
system.switch_cpus.commit.bw_lim_events 10000000 # number cycles where commit BW limit reached
system.switch_cpus.rob.rob_reads 80558432 # The number of ROB reads
system.switch_cpus.rob.rob_writes 43430539 # The number of ROB writes
system.switch_cpus.timesIdled 37218 # Number of times that the entire CPU went into an idle state and unscheduled itself
system.switch_cpus.idleCycles 2755508 # Total number of cycles that the CPU has spent unscheduled due to idling
system.switch_cpus.committedInsts 10000000 # Number of Instructions Simulated
system.switch_cpus.committedOps 10000000 # Number of Ops (including micro ops) Simulated
system.switch_cpus.cpi 8.369191 # CPI: Cycles Per Instruction
system.switch_cpus.cpi_total 8.369191 # CPI: Total CPI of All Threads
system.switch_cpus.ipc 0.119486 # IPC: Instructions Per Cycle
system.switch_cpus.ipc_total 0.119486 # IPC: Total IPC of All Threads
system.switch_cpus.int_regfile_reads 21773538 # number of integer regfile reads
system.switch_cpus.int_regfile_writes 9447282 # number of integer regfile writes
我想找到以下变量并打印出对应的值:
list=(IPC CPI)
IPC="system.switch_cpus.ipc"
CPI="system.switch_cpus.cpi"
for i in $list:
do
awk -v a="$i" '{$1 == $a} {print}' $1
done
然后我使用以下命令运行脚本:
./parser.sh foo.txt
这是打印整个文件。
Output:
system.switch_cpus.commit.op_class_0::total 10000000 # Class of committed instruction
system.switch_cpus.commit.bw_lim_events 10000000 # number cycles where commit BW limit reached
system.switch_cpus.rob.rob_reads 80558432 # The number of ROB reads
system.switch_cpus.rob.rob_writes 43430539 # The number of ROB writes
system.switch_cpus.timesIdled 37218 # Number of times that the entire CPU went into an idle state and unscheduled itself
system.switch_cpus.idleCycles 2755508 # Total number of cycles that the CPU has spent unscheduled due to idling
system.switch_cpus.committedInsts 10000000 # Number of Instructions Simulated
system.switch_cpus.committedOps 10000000 # Number of Ops (including micro ops) Simulated
system.switch_cpus.cpi 8.369191 # CPI: Cycles Per Instruction
system.switch_cpus.cpi_total 8.369191 # CPI: Total CPI of All Threads
system.switch_cpus.ipc 0.119486 # IPC: Instructions Per Cycle
system.switch_cpus.ipc_total 0.119486 # IPC: Total IPC of All Threads
system.switch_cpus.int_regfile_reads 21773538 # number of integer regfile reads
system.switch_cpus.int_regfile_writes 9447282 # number of integer regfile writes
如何在 shell 中创建具有自己值的变量列表,并使用 awk 或 sed 从文件中解析每个变量?
【问题讨论】:
-
i=cpi时的预期输出是什么? -
也许一个简单的 egrep 就可以了?
egrep -iw 'system.switch_cpus.ipc|system.switch_cpus.cpi' foo.txt -
预期输出是包含“foo.txt”中 cpi 值的整行或第二个变量。我想我以后可以在 teh awk 中更改该设置。
-
@ViniciusPlacco:不幸的是,我的变量列表越来越多。在这个特定的问题中,我只展示了 2 个变量。 egrep 选项很酷,但是当搜索 10 个变量时,它可能会变得棘手且容易出错:-(
-
好的,我明白了!您可能会从这篇文章中获得有关
grep的一些信息:stackoverflow.com/questions/17863301/…。祝你好运!