0

I'm new to bash scripting and im trying to make a script that gets the current networking up and download. Im struggeling to understand tools like awk or sed to extract the information i need.

cat /proc/net/dev

Inter-|   Receive  |  Transmit face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
    lo: 2245514   26732    0    0    0     0          0         0  2245514   26732    0    0    0     0       0
 0
 wlan0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0
 0
  eth0: 501233000800 372656590    0  354    0     0          0    478724 19824791855 194196655    0    0    0     0       0          0

this is my output and i need to the the Receive and Transmit field from eth0, i tried using grep -i eth0 to nail it down to only one line, but after this im stuck.

eth0: 501233200415 372657763    0  356    0     0          0    478834 19825031691 194197387    0    0    0     0       0          0

This is how far i've come, how would extract the "501233200415" and "478834" ?

0

If the fields you want are always the 2nd and 9th fields (as in your example, you can use:

grep -i eth0 your_file | awk '{print $2, $9}'
4
  • 3
    awk can do grep job too, like you can do awk '/eth0:/ { print $2, $9 }'. for case insensitive match you can do awk '/[eE][tT][hH]0:/ { print $2, $9 }' or in GNU awk there is IGNORECASE that can enable that. Jul 4 at 18:35
  • Thanks, from what i've read it looked so much more complicated!
    – Crylia
    Jul 4 at 18:50
  • @αғsнιη The site (grymoire.com/Unix/Sed.html) explains sed very well - also it has a fantastic set of other unix tutorials (like awk and grep and so on (grymoire.com/Unix/index.html). Cheers!
    – mattb
    Jul 4 at 20:08
  • 1
    @αғsнιη or awk 'tolower($0) ~ /eth0:/ { print $2, $9 }' for case-insensitive matching with any awk. FWIW I'd just do awk '$1=="eth0:"{print $2, $9}' file for this for simplicity, efficiency and robustness (the existing solution would fail if meth0 or eth03 or similar existed in the input).
    – Ed Morton
    Jul 4 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.