Set multiple variables to different fields of awk output

Kahn

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 grepping the same file 7 times, is there a way to grep this a single time and set these variables to each individual awk output?

Kahn

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.

edited at
0

Comments

0 comments
Login to comment

Related

Output a field to multiple json fields

awk outputs to multiple shell variables

Set the value of multiple variables

match two files with awk and output selected fields

Use multiple delimiters with awk and keep track of $0 as var to sort different fields

Bash AWK find multiple patterns and assign to different variables

Result Set to multiple variables

awk code to output lines with matching fields, but no output

awk and for-loop with multiple variables

Dummy Variables on training and testing set resulting in different size dataframe output

Print multiple fields in AWK but split one of them based on a different delimiter

awk: Output to different processes

awk searching for multiple fields

Test if multiple variables are set

awk assign to multiple variables at once

How to set multiple line command output in different variables? (Batch)

awk multiple conditions output order

Awk extract fields with multiple separator

Filter out different fields for each line with AWK

awk multiple strings in multiple fields

AWK substract different fields in different rows if consecutive fields are identical

Processing multiple file with different number of fields using awk

Assigning output from awk to variables

In ash how to set multiple line output to variables

Copying variables to local text file from multiple ssh awk output

Conditional output with multiple variables based on filled input fields

Conditional matching with multiple fields comparison in different files using AWK

Combine multiple fields with awk

awk print variables with number of fields together

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive