I have a large set of variable initializations that I think could be greatly reduced. The file below being parsed:
-------------------- ACL Stats Per Interface ----------------------
Entries Packets Dropped
Recent Total PerMax Recent Total PerMax
Slot 0 /Port 0
Trusted 1 196 1311578 386 0 0 0
Untrusted 3 20 217217953 852794 0 0 0
... and the snippet of code in the script are below:
expect_results="stats.txt"
acl_stats_per_interface_trusted_entries_s0_p0=`grep -A 2 "Slot 0 /Port 0" $expect_results | grep "Trusted" | awk '{print $2}' `
acl_stats_per_interface_trusted_recent_packets_s0_p0=`grep -A 2 "Slot 0 /Port 0" $expect_results | grep "Trusted" | awk '{print $3}' `
acl_stats_per_interface_trusted_total_packets_s0_p0=`grep -A 2 "Slot 0 /Port 0" $expect_results | grep "Trusted" | awk '{print $4}' `
acl_stats_per_interface_trusted_permax_packets_s0_p0=`grep -A 2 "Slot 0 /Port 0" $expect_results | grep "Trusted" | awk '{print $5}' `
acl_stats_per_interface_trusted_dropped_recent_s0_p0=`grep -A 2 "Slot 0 /Port 0" $expect_results | grep "Trusted" | awk '{print $6}' `
acl_stats_per_interface_trusted_dropped_total_s0_p0=`grep -A 2 "Slot 0 /Port 0" $expect_results | grep "Trusted" | awk '{print $7}' `
acl_stats_per_interface_trusted_dropped_permax_s0_p0=`grep -A 2 "Slot 0 /Port 0" $expect_results | grep "Trusted" | awk '{print $8}' `
Rather than grep
ping the same file 7 times, is there a way to grep this a single time and set these variables to each individual awk
output?
I have opted to read the output of my grep
statement into an array, and access these elements individually, rather than instantiate so many variables.
acl_stats_trusted=( $(grep "Trusted" "$expect_results") )
echo "${acl_stats_trusted[1]}" # Outputs 1
echo "${acl_stats_trusted[3]}" # Outputs 1311578
This is much more concise and clear to any user.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments