我可以帮忙翻译,以下是您需要翻译的内容:
这个脚本会执行以下操作:
我有一个Shell脚本中的解析器:
这是要从中解析的输入文件(input.txt):
input.txt:
system.switch_cpus.commit.swp_count 0 # Number of s/w prefetches committed
system.switch_cpus.commit.refs 2682887 # Number of memory references committed
system.switch_cpus.commit.loads 1779328 # Number of loads committed
system.switch_cpus.commit.membars 0 # Number of memory barriers committed
system.switch_cpus.commit.branches 921830 # Number of branches committed
system.switch_cpus.commit.vec_insts 0 # Number of committed Vector instructions.
system.switch_cpus.commit.fp_insts 0 # Number of committed floating point instructions.
system.switch_cpus.commit.int_insts 10000000 # Number of committed integer instructions.
这个脚本会执行以下操作:
$ cpu1_name="system.switch_cpus"
$ echo "$(grep "${cpu1_name}.commit.loads" ./input.txt |grep -Eo '[0-9]+')"
correct expected output: 1779328
但是在另一个文件中,“cpu1_name”变量更改为“system.switch_cpus_1”。现在运行相同的脚本会给我2个值:
New input file:
system.switch_cpus_1.commit.swp_count 0 # Number of s/w prefetches committed
system.switch_cpus_1.commit.refs 2682887 # Number of memory references committed
system.switch_cpus_1.commit.loads 1779328 # Number of loads committed
system.switch_cpus_1.commit.membars 0 # Number of memory barriers committed
system.switch_cpus_1.commit.branches 921830 # Number of branches committed
system.switch_cpus_1.commit.vec_insts 0 # Number of committed Vector instructions.
system.switch_cpus_1.commit.fp_insts 0 # Number of committed floating point instructions.
Modified Script line:
$ cpu1_name="system.switch_cpus_1"
$ echo "$(grep "${cpu1_name}.commit.loads" ./new_input.txt |grep -Eo '[0-9]+')"
1
1779328
你可以看到,管道连接的grep命令会查找任何数字,并由于变量名称的更改而报告额外的“1”。
有没有办法只选择数字的第二部分(即仅1779328)?
我知道我可以使用awk'{print $2}
,但这意味着需要在脚本中更改很多行。所以我想知道是否有现有脚本行的更简单的技巧。
提前感谢
-P
标志。 - Martin Heralecký