bug-bash
[Top][All Lists]
Advanced

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

Passing variables to awk


From: TimtheEagle
Subject: Passing variables to awk
Date: Thu, 1 Nov 2007 02:16:32 -0700 (PDT)

Hi all,
having created a complicated (for me) awk statement previously with the
forums' help I am now amending it and trying to pass variables to it.  My
script is as follows with the respective outputs (hash then unhash when
running etc) for:-
1. variable var1 passed containing Framed-IP-Address
and 
2. no variable as the Framed-IP-Address part is hardcoded.

#!/bin/bash
set -o xtrace
#
names[0]=smith

ipaddr[0]="Framed-IP-Address = 10\.6\.6\."

#1.
awk -v var1="${ipaddr[0]}" '/*** Received from 10.242.252.20 port 1645
..../{ last_acct_status = NR; as = $0 }/Code:       Access-Accept/ &&
last_acct_status + 1 == NR {last_user_name = NR; print NR-1 as; print NR $0
}/$var1/ && last_acct_status + 10 == NR { print NR $0 }' /tmp/theout >
/tmp/${names[0]}good

#2.
#awk '/*** Received from 10.242.252.20 port 1645 ..../{ last_acct_status =
NR; as = $0 }/Code:       Access-Accept/ && last_acct_status + 1 == NR
{last_user_name = NR; print NR-1 as; print NR $0 }/Framed-IP-Address =
10\.6\.6\./ && last_acct_status + 10 == NR { print NR $0 }' /tmp/theout >
/tmp/${names[0]}good

cat  /tmp/${names[0]}good

exit 1


Outputs:-

1.  (broken as not delivering Framed-IP-Address output at end)

+ names[0]=smith
+ ipaddr[0]='Framed-IP-Address = 10\.6\.6\.'
+ awk -v 'var1=Framed-IP-Address = 10\.6\.6\.' '/*** Received from
10.242.252.20 port 1645 ..../{ last_acct_status = NR; as = $0 }/Code:      
Access-Accept/ && last_acct_status + 1 == NR {last_user_name = NR; print
NR-1 as; print NR $0 }/$var1/ && last_acct_status + 10 == NR { print NR $0
}' /tmp/theout
awk: warning: escape sequence `\.' treated as plain `.'
+ cat /tmp/smithgood

106*** Received from 10.242.252.20 port 1645 ....
107Code:       Access-Accept
+ exit 1

2.  (working as desired but hardcoded Framed-IP-Address string)

+ names[0]=smith
+ ipaddr[0]='Framed-IP-Address = 10\.6\.6\.'
+ awk '/*** Received from 10.242.252.20 port 1645 ..../{ last_acct_status =
NR; as = $0 }/Code:       Access-Accept/ && last_acct_status + 1 == NR
{last_user_name = NR; print NR-1 as; print NR $0 }/Framed-IP-Address =
10\.6\.6\./ && last_acct_status + 10 == NR { print NR $0 }' /tmp/theout
+ cat /tmp/smithgood

106*** Received from 10.242.252.20 port 1645 ....
107Code:       Access-Accept
116     Framed-IP-Address = 10.6.6.47
+ exit 1

=================================================================
The second output contains the Framed-IP-Address statement which is what I
need.
Although I can see a warning in output 1 about \. being treated as plain . I
have not been able to remove it even though I suspect it would solve the
problem.
Thanks for any help in advance - please tell me if there is a more
appropriate forum.
Cheers Tim
-- 
View this message in context: 
http://www.nabble.com/Passing-variables-to-awk-tf4730133.html#a13525369
Sent from the Gnu - Bash mailing list archive at Nabble.com.





reply via email to

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