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.
How about:
Explanation:
The first script prints all
ent'salong with packets transmitted count:The second script prints only the count for the given
ent:You can use command substitution in bash to store the value in shell variable:
And the
-voption ofawkto pass in variables: