Parsing /proc/net/dev into key:value pairs (self-answered)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing /proc/net/dev into key:value pairs (self-answered)
# 1  
Old 11-18-2008
Computer 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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Cat /proc/sys/net/ipv4/ip_local_port_range

Hello, /proc/sys/net/ipv4/ip_local_port_range returns 32000 - 61000, i have a client TCP and a Server TCP. i have used bind() only on the server, the port of socket client is given by the OS that's it ? it retrieves this port from this range (/proc/sys/net/ipv4/ip_local_port_range) ? ... (5 Replies)
Discussion started by: chercheur111
5 Replies

2. Shell Programming and Scripting

Extracting key/value pairs in awk

I am extracting a number of key/value pairs in awk using following: awk ' /xyz_session_id/ { n=index($0,"xyz_session_id"); id=substr($0,n+15,25); a=$4; } END{ for (ix in a) { print a } }' I don't like this Index + substr with manually calculated... (5 Replies)
Discussion started by: migurus
5 Replies

3. Shell Programming and Scripting

Query related to the SSH key pairs

Hi All, I have question , How to generate the SSH keys for the two servers in the pair. So if the server will be replicated , same SSH key pairs will work on each server. No need to generate the SSH keys for the second server. I have created the SSH keys for the single server. I was... (1 Reply)
Discussion started by: sharsour
1 Replies

4. Shell Programming and Scripting

Parsing line with name:value pairs in shell script

HI , I have the following type of lines in a file and need to create a csv file that can be bcp'ed into a db The problem that I have is the delimited of the <name :value> is a space but some of the values in the pairs have space . eg msg_src_time:03/05/13 10:40:17.919 Need sugesstions on... (9 Replies)
Discussion started by: tasmac
9 Replies

5. UNIX for Advanced & Expert Users

Parse (delimited string) key-value pairs in a column into separate lines

Hi experts, e.g. i/p data looks like 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified|| o/p data should like - row1: 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747|unidentified ... (1 Reply)
Discussion started by: sumoka
1 Replies

6. Shell Programming and Scripting

Searching a delimited Key value pairs in shell script

Hello, I have property file with key value pairs separated by pipe , I am trying to write a script which reads the property file and search and print value of specific key. I tried with Sed, I am successfull. The file is as follows ... (4 Replies)
Discussion started by: ANK
4 Replies

7. UNIX for Dummies Questions & Answers

_/proc/stat vs /proc/uptime

Hi, I am trying to calculate the CPU Usage by getting the difference between the idle time reported by /proc/stat at 2 different intervals. Now the 4th entry in the first line of /proc/stat will give me the 'idle time'. But I also came across /proc/uptime that gives me 2 entries : 1st one as the... (0 Replies)
Discussion started by: coderd
0 Replies

8. Post Here to Contact Site Administrators and Moderators

indicating when a question has been answered?

hi, this is just a suggestion at Sun's java forums, the person who asked the original question has the ability to indicate that their question has been "answered". I think that is quite handy, as it lets people who are here to solely answer questions skip that thread. There is a potential... (5 Replies)
Discussion started by: JamesByars
5 Replies

9. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

10. Post Here to Contact Site Administrators and Moderators

how gollum answered..

Dear admins, i remember, my first thread, it is regarding unix systems and i asked some suggestions... and i like to know how gollum answered for that thread?.. and is gollum is a program?.. (5 Replies)
Discussion started by: sekar sundaram
5 Replies
Login or Register to Ask a Question