I need to extract from /proc/net/dev
the second and tenth columns separated by spaces. In other words, the number of received and transmitted bytes.
Is it possible to do this using grep
and back-references?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityto answer you question:
awk '{print $2,$10}' /proc/net/dev
yield
Receive
|bytes packets
6385384218 42314607745
12034420041 12034420041
which might not be what you are after.
you can use
awk '$1 ~ /:/ {print $2,$10}' dev
6385389751 42314610935
12034420241 12034420241
where
$1 ~ /:/
select line where first filed is foo:{print $2,$10}
print second and tenth field/proc/dev/net
In other word, I see no regular expression applied to a single grep command then will turn
eth0: 6385654376 26986310 0 107680 0 0 0 4665 42314947519 21475282 0 0 0 0 0 0
lo: 12034424441 4553238 0 0 0 0 0 0 12034424441 4553238 0 0 0 0 0 0
to (not exact figure)
6385389751 42314610935
12034420241 12034420241
using grep FOO
where FOO
consist only of grep's options and arguments.
In @Emeric's try he could have two columns, when i run on my OS:
6385672702
42314983073
12034425441
12034425441
yet, you can try your luck at https://codegolf.stackexchange.com/
If all you can use is grep
, have a go at the following:
grep -Eo '[^ ]+:( *[0-9]*){9}' /proc/net/dev | grep -Eo '(: *[0-9]*|[0-9]*$)' | grep -o '[0-9]*'
137517672
9029195
As pointed out earlier, grep
is far from being the best way to achieve this.
awk '{print $2,$10}' dev
grep
is the tool to print the lines that match a regex, (that's where the name comes from), not to extract data from the lines (though some implementations now have a-o
option that can do it to some extent).