user3285014
user3285014

Reputation: 319

Bash Text Parsing

I am getting confused with parameter substitution. Basically the file to be parsed is in the following structure:

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

I want to find the following variables and print out the corresponding value:

    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

Then I run the script with the following command:

./parser.sh foo.txt

This is printing out the whole file.

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

How can create a list of variables in shell who have their own values and parse each of them from a file using awk or sed?

Upvotes: 2

Views: 354

Answers (2)

karakfa
karakfa

Reputation: 67467

you can do all in one awk script, if your list is not in a file you can use a here document and file substitution as below.

$ awk 'NR==FNR{a[$1]; next} $1 in a' <(cat << EOF
system.switch_cpus.ipc
system.switch_cpus.cpi
EOF
) file

will give you

system.switch_cpus.cpi                       8.369191                       # CPI: Cycles Per Instruction
system.switch_cpus.ipc                       0.119486                       # IPC: Instructions Per Cycle

If you want to search one at a time with a variable

$ var='system.switch_cpus.ipc'; awk -v var="$var" '$1==var' file

system.switch_cpus.ipc                       0.119486                       # IPC: Instructions Per Cycle

However, in that case, using grep might be better off

$ var='system.switch_cpus.ipc'; grep -wF "$var" file

system.switch_cpus.ipc                       0.119486                       # IPC: Instructions Per Cycle

UPDATE

If your variable names are in a list, you can decode the values with this

$ vars=(var1 var2)        # define the list with variables, values even may not be assigned yet
$ var1=value1; var2=value2                   # assign values
$ for v in ${vars[@]}; do echo ${!v}; done   # extract values with indirect reference
value1
value2

Upvotes: 2

LMC
LMC

Reputation: 12662

This code will print the entire record

awk -v a="$i" '{ if($1 == a){print $0} }' $1

One of the problems with your code is that variable a must not be preceded by $ in awk.

Upvotes: 0

Related Questions