Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:50:28+00:00 2026-06-18T05:50:28+00:00

I have output from a command in the following format: Ethernet STATISTICS (ent0) :

  • 0

I have output from a command in the following format:

Ethernet STATISTICS (ent0) :
Device Type: 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
Hardware Address: 00:09:6b:6e:5d:50
Transmit Statistics:                          Receive Statistics:
--------------------                          -------------------
Packets: 0                                    Packets: 0
Bytes: 0                                      Bytes: 0
Interrupts: 0                                 Interrupts: 0
Transmit Errors: 0                            Receive Errors: 0
Packets Dropped: 0       
ETHERNET STATISTICS (ent1) :
Device Type: 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)
Hardware Address: 00:09:6b:6e:5d:50
Transmit Statistics:                          Receive Statistics:
--------------------                          -------------------
Packets: 30                                   Packets: 0
Bytes: 1800                                   Bytes: 0
Interrupts: 0                                 Interrupts: 0
Transmit Errors: 0                            Receive Errors: 0
Packets Dropped: 0                            Packets Dropped: 0
                                              Bad Packets: 0

I need to save to variables the number of packets transmitted associated with ent0 and the number of packets transmitted associated with ent1. I need to use awk for this task and while I know how to extract the number of packets, I don’t know how to associate it with the adapter (ent0 or ent1) listed several lines above it. Seems like I need to use some kind of nested loops but don’t know how to do this in awk.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:50:29+00:00Added an answer on June 18, 2026 at 5:50 am

    How about:

    # list all ent's and there counts 
    $ awk '/ent[0-9]+/{e=$3}/^Packets:/{print e,$2}' file
    (ent0) 0
    (ent1) 30
    
    # list only the count for a given ent 
    $ awk '/ent0/{e=$3}/^Packets:/&&e{print $2;exit}' file
    0
    
    $ awk '/ent1/{e=$3}/^Packets:/&&e{print $2;exit}' file
    30
    

    Explanation:

    The first script prints all ent's along with packets transmitted count:

    /ent[0-9]+/        # For lines that contain ent followed by a digit string
    {
       e=$3            # Store the 3rd field in variable e
    }
    /^Packets:/        # Lines that start with Packet:
    {
       print e,$2      # Print variable e followed by packet count (2nd field)
    }
    

    The second script prints only the count for the given ent:

    /ent0/             # For lines that match ent0
    {
       e=$3            # Store the 3rd field 
    }
    /^Packets:/&&e     # If line starts with Packets: and variable e set 
    {
       print $2        # Print the packet count (2nd field)
       exit            # Exit the script 
    }
    

    You can use command substitution in bash to store the value in shell variable:

    $ entcount=$(awk '/ent1/{e=$3}/^Packets:/&&e{print $2;exit}' file)
    
    $ echo $entcount 
    30
    

    And the -v option of awk to pass in variables:

    $ awk -v var=ent0 '$0~var{e=$3}/^Packets:/&&e{print $2;exit}' file 
    0
    
    $ awk -v var=ent1 '$0~var{e=$3}/^Packets:/&&e{print $2;exit}' file 
    30
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following output from the uptime command: 14:48:51 up 23:34, 2 users,
I have a NUL delimited output coming from the following command : some commands
Possible Duplicate: Running a command from Ruby displaying and capturing the output I have
If I get an output from a command that outputs information in the following
I have output from grep I'm trying to clean up that looks like: <words>Http://www.path.com/words</words>
i have output from a server like [alex, \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd] i want to convert it
I am trying to debug a segfault, and I have this output from gdb:
i have an xml output from a webservice in a variable results it looks
I have a directory OUTPUT where I have the output files from a Map
I have an array of output from a database. I am wondering what the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.