Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with AWK!

Status
Not open for further replies.

psychosb

Technical User
Oct 19, 2011
3
Hello there!

I'm wondering if anyone can help me with this little (big) challenge!

I have a file, with about 30 blocks, each one with the same structure as the following:

---

Timestamp: 14:52:50

+++++ STCP DEVICE METERS +++++

netstat -interface #sdlmux.m5.13.1.0

MAC Type : CSMA/CD
MAC Address: 00:00:a8:43:3f:cd
Device Name: #sdlmux.m5.13.1.0
Line Speed : 100 mb/s
Line Duplex: Full-Duplex

MAC Statistics:
Received frames : 3066396
Received multicast and broadcast frames : 102
Received octets : 813041981
Transmitted frames : 3430870
Transmitted octets : 1220638884
LAN Chipset re-initialized : 0
SQE error : 0
Transmit ring full : 0
Transmit frame discarded, late collisions: 0
Transmit frame was deferred : 0
Transmit frame after a single retry : 0
Transmit frame after multiple retry : 0
Transmit frame discarded, excessive retry: 0
Receive frame discarded, lack of buffers : 0
Receive frame discarded, improper framing: 0
Receive frame discarded, an overflow : 0
Receive frame discarded, bad CRC : 0
Receive frame discarded, bad address : 0
Receive frame discarded, congestion : 0

MAC Summary:
Transmitted frames : 3430870
Transmitted octets : 1220638884
Retransmitted frames : 0
Received frames : 3066498
Received octets : 813041981
Total of lost frames : 0
ready 18:52:49


***** END OF RECORD *****


I'm wondering if there's a way to use AWK (and SED) to get the values above in red, and put them in a line, separated with comma... like this:

Timestamp , device_name , received_frame , received_multicast , received_octets , transmitted_frames , transmitted_octets , Receive_frame_bad_crc

This is getting me nuts. I have to get the line for every block in the text file, and I can't use the awk to get the column values, because it's not allways the same.

Anyone can help me?

Thanks!!!
 
Something like this should do the trick, I'll leave you to fill in the dots.

Code:
awk '
    [green]/^Device Name/[/green] { dev=[blue]$NF[/blue] }
    [green]/^Timestamp/[/green] { ts=[blue]$NF[/blue] }
    [green]/^Received Frames/[/green] { rf=[blue]$NF[/blue] }
    [gray]# ...[/gray]
    [green]/ END OF RECORD /[/green] {
        [b]print[/b] ts, dev, rf [gray]# ...[/gray]
    }
' inputfile

If any of the device names contain spaces you may need to do something a little more clever than using $NF (which means the last field on the line) to capture them.


Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top