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 9 of 14 Topic 2115 of 2341
Post > Topic >>

Re: Pattern matching a Shell variable inside awk!!

by Grant <g_r_a_n_t_@[EMAIL PROTECTED] > Dec 27, 2007 at 10:05 AM

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?

BEGIN {
        ARGV[ARGC++] = "/etc/netdraw.conf" # grab configuration file
        ARGV[ARGC++] = "/proc/net/dev"  # grab interface status
}
/^$/    { next }
/^#/    { next }
# read configuration
NR == FNR { conf[$1] = $2 }
NR == FNR && $1 == "samplefile" {
        out = $2
        printf "%s ", strftime("%F.%H:%M") >> out
}
# read network status
NR != FNR && $0 ~ conf["interface"] ":" {
        ++f
        sub(/:/, " ")
        print $2, $10 > out
}
END {
        if (!f)
                print 0, 0 > out
        close(out)
}

Grant.
-- 
http://bugsplatter.mine.nu/
 




 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 Wed Aug 27 17:39:03 CDT 2008.