The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl parsing compared to Ksh parsing popeye Shell Programming and Scripting 1 08-06-2008 10:46 PM
reading lines in pairs from file in ksh ytokar Shell Programming and Scripting 4 02-08-2008 02:50 PM
perl search and replace pairs umen Shell Programming and Scripting 1 07-30-2006 11:37 AM
proc bache_gowda UNIX for Dummies Questions & Answers 7 05-23-2005 11:18 AM
/proc aojmoj UNIX for Advanced & Expert Users 3 11-20-2002 05:54 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2008
felbane felbane is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 1
Talking Parsing /proc/net/dev into key:value pairs (self-answered)

Hi all,

I need some help with using sed/awk/other linux tools to meet the following goal:

I'm trying to take the output of /proc/net/dev:
Code:
Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo:18748525  129811    0    0    0     0          0         0 18748525  129811    0    0    0     0       0          0
  eth0:1699369069 226296437    0    0    0     0          0      3555 4118745424 194001149    0    0    0     0       0          0
  eth1:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
  sit0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
...grep out an interface (eth0) and turn it into the following data:
Code:
rxbytes:1699369069 rxpackets:226296437 rxerrs:0 rxdrop:0 rxfifo:0 rxframe:0 rxcompressed:0 rxmulticast:3555 \
txbytes:4118745424 txpackets:194001149 txerrs:0 txdrop:0 txfifo:0 txcolls:0 txcarrier:0 txcompressed:0
I've gotten as far as this snippet:
Code:
cat /proc/net/dev | egrep "(eth0|face)" | sed -e 's/|/:/' | sed -e 's/|/ /' | cut -d ":" -f 2 | tr -s " " " "
 
bytes packets errs drop fifo frame compressed multicast bytes packets errs drop fifo colls carrier compressed
1709184740 226328683 0 0 0 0 0 3555 4122564415 194035769 0 0 0 0 0 0
... but I'm not sure where to proceed from here, or if this is even the right track. Has anyone done something similar before (i.e. turning columns into key:value pairs)?

Your expertise is much appreciated!

Update: I've found how to transpose rows into columns (which is a step closer!):
Code:
cat /proc/net/dev | egrep "(eth0|face)" | sed -e 's/|/:/' | sed -e 's/|/ /' | cut -d ":" -f 2 | tr -s " " " " | \
awk 'BEGIN {FS=" "} {for (i=1;i<=NF;i++){ arr[NR,i]=$i; if(big <= NF) big=NF; }} \
END {for(i=1;i<=big;i++){for(j=1;j<=NR;j++){printf("%s:",arr[j,i]);}printf("\n");}}'

bytes:1718395341:
packets:226353349:
errs:0:
drop:0:
fifo:0:
frame:0:
compressed:0:
multicast:3555:
bytes:4126856358:
packets:194063589:
errs:0:
drop:0:
fifo:0:
colls:0:
carrier:0:
compressed:0:
Now I just need to figure out how to prepend the 'tx' and 'rx' and strip the trailing ':' and I'm all set! This forum is awesome.

Update 2:
I got it! The only way it could be better is with CamelCase (RxBytes, etc):
Code:
cat /proc/net/dev | egrep "(eth0|face)" | sed -e 's/|/:/' -e 's/|/ /' | cut -d ":" -f 2 | tr -s " " " " | \
awk 'BEGIN {FS=" "} {for (i=1;i<=NF;i++){ if(i<9){arr[NR,i]="rx"$i;}else{arr[NR,i]="tx"$i;} if(big <= NF) big=NF; }} \
END {for(i=1;i<=big;i++){for(j=1;j<=NR;j++){ printf("%s\t",arr[j,i]);}printf("\n");}}' | sed -e 's/\t$//' -e 's/\t/:/' -e 's/:[tr]x/:/'
rxbytes:1790844622
rxpackets:226585666
rxerrs:0
rxdrop:0
rxfifo:0
rxframe:0
rxcompressed:0
rxmulticast:3555
txbytes:4161116750
txpackets:194310608
txerrs:0
txdrop:0
txfifo:0
txcolls:0
txcarrier:0
txcompressed:0
The multiline format actually works well for my application.

Last edited by otheus; 01-30-2009 at 07:31 AM.. Reason: Woot!
Closed Thread

Bookmarks

Tags
unresolved, unresolved for months, unresolved for weeks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0