Evaluating a variable within a variable

2007-10-23 Thread TimtheEagle

Hi,

my problem is best desrbied with a simple example as follows.  I know that
technically it is not correct but it illustrates what I am struggling to
achieve.


#

main_auth=7

f=main
t=auth

ile=$f"_"$t

echo $ile



I want the output from echo on the line above to be 7.  The main_auth=7 will
be read from a separate file in due course.

I hope that is clear enough.

Any help greatly appreciated.

Thanks  Cheers Tim 
-- 
View this message in context: 
http://www.nabble.com/Evaluating-a-variable-within-a-variable-tf4674649.html#a13355804
Sent from the Gnu - Bash mailing list archive at Nabble.com.





Passing variables to awk

2007-11-01 Thread TimtheEagle

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.