help-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: cga2000
Subject: Specifying multiple separators via FS or the -F command line flag
Date: Sun, 02 Dec 2007 14:09:23 -0500
User-agent: Mutt/1.5.13 (2006-08-11)

I wrote a bunch of one-liners that display system-monitoring info in my
gnu/screen hardstatus line.

Here's some screenshots and my "backticks":

http://www.geocities.com/fcky1000/fcky

This one monitors network interface eth0:

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")}}'

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

The following two blocks are the left/right half of the input file:

$ cat /proc/net/dev

Inter-|   Receive                                                
 face |bytes    packets errs drop fifo frame compressed multicast
    lo:649004371  349860    0    0    0     0          0         
  eth0:673423996 22297294  789    0 1100   789          0     <-

|  Transmit
|bytes    packets errs drop fifo colls carrier compressed
0 649004371  349860    0    0    0     0       0          0
 0 50756739 3811203    0    0    0     0 3465469          0

There is one annoying problem:

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 thought I would be able to get away with the following tactics:

Specifying space OR colon as the separator so that it doesn't matter if
the eth0 line contains:

<space><space><eth0><:><space><space> .. <byte-counter>

.. or

<space><space><eth0><:><byte-counter>

What I hoped was that since "eth0" is followed by ":" and zero, one,
two, three, etc. <spaces> this would work with $4 always mapping to the
byte-counter field, thus saving me some involved parsing that somewhat
defeats the purpose of using awk in the first place.

Well, unfortunately, it works when there are no spaces between the
colon and the byte counter but it doesn't when there are intervening
spaces.

I don't know whether this is due to the fact that there's something
wrong with my -F flag syntax (maybe it does not mean "colon or space"
or whether this is due to awk testing a single colon or space, not any
succession of colons and spaces.

IOW, if there are three intervening spaces between ":" and the byte
counter, the three spaces count as $4, $5, and $6 .. and my byte
counter becomes $7.

Naturally since there is room for 9 digits and therefore up to 8 spaces,
this second explanation would make an initially trivial problem rather
more complicated and possibly not a good candidate for awk.

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.

Thank you.







reply via email to

[Prev in Thread] Current Thread [Next in Thread]