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?
read -a acl_stats_per_interface_trusted_s0_p0
) rather than individual variables. Also remember thatawk
can do regex matching - there's rarely any good reason to chain grep and awk.