1

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?

4
  • is sed mandatory ? awk is more suited for that kind of work.awk '{print $2,$10}' dev
    – Archemar
    Aug 3, 2015 at 13:40
  • I need exactly the regular expression for grep. I believe that its possible but dont know how.
    – Guest
    Aug 3, 2015 at 13:46
  • For sed the following expression works s/( [^ ])( [^ ])( [^ ]){7}( [^ ]).*/\2\4/. But for egrep it does not work
    – Guest
    Aug 3, 2015 at 13:57
  • 2
    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). Aug 3, 2015 at 14:30

2 Answers 2

1

to 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

grep

  1. grep can be use to match a regular expression, so you can pick numbers from /proc/dev/net
  2. however, regular expression describe but cannot act.

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/

1
  • Archemar nailed it in the first line.
    – Baazigar
    Aug 3, 2015 at 15:32
0

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
  • First expression prints the interface name + colon, followed by the next 9 groups of digits.
  • Second expression extracts only the groups of digits preceded by the colon or followed by the end of line (First and last selected columns). This is where the line is split into one line per matching column.
  • Last expression gets rid of the leading colon (Nothing political here).

As pointed out earlier, grep is far from being the best way to achieve this.

2
  • That do not answer OP's question, see my edit.
    – Archemar
    Aug 3, 2015 at 14:36
  • Good point. I missed the 'seprated by spaces' part.
    – Emeric
    Aug 3, 2015 at 14:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .