How to find two values ​​in a file and use them as variables?

How can I find and save two numbers as variables, followed by "RX bytes:" and "TX bytes:" in this file? I want to calculate using these values ​​in a simple monitor the current bandwidth of a bash script using an OpenWrt router.

/ DEV / band1:

 br-lan Link encap:Ethernet HWaddr inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3848954 errors:0 dropped:21234 overruns:0 frame:0 TX packets:4213574 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1206316927 (1.1 GiB) TX bytes:3385060741 (3.1 GiB) 

Thank you for your help!

0
bash regex grep awk openwrt
May 29 '13 at 13:35
source share
2 answers

for example RX bytes, you could:

 rxBytes=$(yourcmd|grep -Po '(?<=RX bytes:)\d+') 

replace RX with TX, you will get another variable

EDIT

you can also go with awk:

 rxBytes=$(awk -F'RX bytes:' 'NF>1{sub(/ .*$/,"",$2);print $2}') 

chg RX β†’ TX to get another.

+3
May 29 '13 at 1:40 pm
source share
 #!/bin/bash N=(`ifconfig p2p1 | sed -n 's/.*RX bytes:\([0-9]*\) .*TX bytes:\([0-9]*\).*/\1\n\2/p'`) echo Bytes received ${N[0]} echo Bytes sent ${N[1]} 

This is done with a single ifconfig call, which is probably only important if you want to poll counters at the same time.

+1
May 29 '13 at 1:47
source share



All Articles