I have a parser in a shell script:
Here is the input file for analysis from (input.txt):
input.txt:
system.switch_cpus.commit.swp_count 0
system.switch_cpus.commit.refs 2682887
system.switch_cpus.commit.loads 1779328
system.switch_cpus.commit.membars 0
system.switch_cpus.commit.branches 921830
system.switch_cpus.commit.vec_insts 0
system.switch_cpus.commit.fp_insts 0
system.switch_cpus.commit.int_insts 10000000
The script does the following:
$ cpu1_name="system.switch_cpus"
$ echo "$(grep "${cpu1_name}.commit.loads" ./input.txt |grep -Eo '[0-9]+')"
correct expected output: 1779328
But in another file, the variable "cpu1_name" changes to "system.switch_cpus _1 " Executing the same script now gives me 2 values:
New input file:
system.switch_cpus_1.commit.swp_count 0
system.switch_cpus_1.commit.refs 2682887
system.switch_cpus_1.commit.loads 1779328
system.switch_cpus_1.commit.membars 0
system.switch_cpus_1.commit.branches 921830
system.switch_cpus_1.commit.vec_insts 0
system.switch_cpus_1.commit.fp_insts 0
Modified Script line:
$ cpu1_name="system.switch_cpus_1"
$ echo "$(grep "${cpu1_name}.commit.loads" ./new_input.txt |grep -Eo '[0-9]+')"
1
1779328
As you can see, grep traffic searches for any number and reports "1" due to the changed variable name.
Is there a way to select only the second part of the number (i.e. only 1779328)? I know what I can use awk'{print $2}, but that would mean changing a lot of lines in the script. So I wondered if there was a simpler trick with existing script lines.
Thanks in advance