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.