Re: Specifying multiple separators via FS or the -F command line flag

[email protected] (Bob Proulx)
Newsgroups gmane.comp.gnu.utils
Message-ID <[email protected]>
cga2000 wrote:
> backtick 4 0  0  awk -F "[: ]" 'BEGIN                              \
> {file="/proc/net/dev";while(a==a){while(getline<file!=0){if        \
> ($3=="eth0"){d=$4;u=$12;printf " %4.1f k/s ",((d-dp)/1024);printf  \
> "%4.1f k/s\n",((u-up)/1024)}};close(file);dp=d;up=u;system("sleep  \
> 1")}}'

Oh, gosh, that is hard to read!  But of course you already knew that. :-)

> NB. I do realize that when one-liners get to be this long it's probably
>     time to move them to a file :-)

Yes.

>     lo:649004371  349860    0    0    0     0          0         
>   eth0:673423996 22297294  789    0 1100   789          0     <-
> When I bring up the interface the byte counter is separated from
> field "eth0:" by a number of spaces.
> After a while as the value become larger, and eventually all the
> spaces disappear to make room for the increased number of bytes.

I tend to pre-process these cases to guarantee space separation.

  line=' eth0:673423996 22297294  789    0 1100   789          0'
  echo "$line" | awk '{sub(/(eth[[:digit:]]|lo):/,"& ");print $2}'
  673423996

  line='  lo:649004371  349860    0    0    0     0          0'
  echo "$line" | awk '{sub(/(eth[[:digit:]]|lo):/,"& ");print $2}'
  673423996

I would try that technique.  Here I have used an ERE to match either
of the cases presented.  It is still not very general though.  I would
tend to get the list of devices dynamically.

I don't know if this is a great way to do this but so far I have
noticed that every type of system I have tried reports header lines in
capital letters.  I can use this to detect which lines are device
lines.

  netstat -ni | awk '/^[[:lower:]]/{print$1}' | sort -u
  eth1
  eth2
  lo

I would probably use that to build the match regular expression.  But
I will leave that as an exercise for the reader.  :-)

> I thought I'd use the above to illustrate my problem rather than a test
> file as Bob Proulx suggested in his reply to my initial post in the
> bug-gnu-utils list because I have no knowledge of awk apart from reading
> through an online tutorial a couple of months ago and I might have made
> some assumptions that would have confused the issue further.

But the full case is often very intimidating.  That big block of awk
one-liner is too dense.  It scares people off.

I would tend not to use "only" awk here.  My own preference is to use
shell scripts and use awk in pieces.  Also, instead of parsing /proc I
would use netstat because it is more portable.  I am not going to
finish this off but you can see where I am going with this:

  netstat -ni | awk '/^[[:lower:]]/{print$1,$4}'

Hope that helps,
Bob
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.