Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Pattern mat...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 14 Topic 2115 of 2379
Post > Topic >>

Re: Pattern matching a Shell variable inside awk!!

by Ed Morton <morton@[EMAIL PROTECTED] > Dec 26, 2007 at 11:52 PM

On 12/26/2007 5:05 PM, Grant wrote:
> On Wed, 26 Dec 2007 21:36:48 +0000 (UTC), gazelle@[EMAIL PROTECTED]
(Kenny McCormack) wrote:
> 
> 
>>In article <ppf5n3pkc4v6elkicjp8arsrff7rp6vrtg@[EMAIL PROTECTED]
>,
>>Grant  <bugsplatter@[EMAIL PROTECTED]
> wrote:
>>...
>>
>>>Move the first tick:
>>># awk /$VAR/ '{ print $0 }' myfile
>>
>>Others will chime in and say that this is a bad idea (security, etc,
etc).
>>That is VAR has some weird value (with "rm -rf" and such in it), it will
>>cause something horrible to happen, etc, etc.
>>
>>But the fact is, it is perfectly safe for normal user code; you can
>>usually control it OK, and, more to the point, the alternative, using
>>"-v" is just as vulnerable to the same hack(s).
>>
>>The only truly safe way to do it is to pass it in the environment.
>>Well, that's safe in an AWK sense, but of course, you then have to worry
>>about the syntax of your "ex****t foo=..." statement...
>>
>>But the real answer is that you almost certainly don't need to do this
>>at all, if you are using AWK correctly.  I've found that most of the
>>time, when this question is asked, the answer is "Learn and use AWK
>>correctly, and you won't need to do it".
> 
> 
> Okay, I have this running for some weeks as a cron job:
> 
> # pull rx and tx bytes from /proc/net/dev:
> #         rx                        tx
> # ppp0:22921966 17725 0 0 0 0 0 0 444056 10437 0 0 0 0 0 0
> #
> # output line: date rx_bytes tx_bytes, or date 0 0 when interface down
> #
> CONF="/etc/netdraw.conf"
> INTERFACE=$(awk '$1=="interface"{print $2}' $CONF)
> SAMPLEFILE=$(awk '$1=="samplefile"{print $2}' $CONF)
> #
> printf "%s " $(date +%F.%H:%M) >> $SAMPLEFILE
> 
> awk /$INTERFACE:/'{f++;sub(/:/," ");print $2,$10};END{if(!f)print 0,0}'
\
> 	/proc/net/dev >> $SAMPLEFILE
> 
> Bash, if it matters, yes I can do the whole thing in awk but it looks
even 
> uglier?

It's hard to say for sure without knowing what's in /etc/netdraw.conf and
some
more sample input+output, but it looks like this will do what you want:

awk 'NR==FNR {
   if ($1 == "interface") interface=$2
   if ($1 == "samplefile") samplefile=$2
   next
}
$0 ~ interface":" { rx=$2; tx=$10 }
END { print strftime("%F.%H:%M"),rx+0,tx+0 > samplefile }'
/etc/netdraw.conf
/proc/net/dev

which doesn't seem particularly ugly to me, but in any case Kenny's point
wasn't
that you should abandon everything else and only write awk, just that if
you're
going to use awk it's worth taking the time to learn to use it correctly.

	Ed.
 




 14 Posts in Topic:
Pattern matching a Shell variable inside awk!!
fazlin <fazlincse@[EMA  2007-12-26 06:12:22 
Re: Pattern matching a Shell variable inside awk!!
Janis Papanagnou <Jani  2007-12-26 16:15:52 
Re: Pattern matching a Shell variable inside awk!!
fazlin <fazlincse@[EMA  2007-12-26 09:26:07 
Re: Pattern matching a Shell variable inside awk!!
Janis Papanagnou <Jani  2007-12-26 19:20:53 
Re: Pattern matching a Shell variable inside awk!!
fazlin <fazlincse@[EMA  2007-12-26 12:46:42 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-27 07:54:50 
Re: Pattern matching a Shell variable inside awk!!
gazelle@[EMAIL PROTECTED]  2007-12-26 21:36:48 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2007-12-26 16:18:02 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-27 10:05:25 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2007-12-26 23:52:10 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-28 08:00:25 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2007-12-27 17:57:59 
Re: Pattern matching a Shell variable inside awk!!
Grant <g_r_a_n_t_@[EMA  2007-12-28 12:54:06 
Re: Pattern matching a Shell variable inside awk!!
Ed Morton <morton@[EMA  2008-01-02 09:50:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Oct 16 0:59:55 CDT 2008.