Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK multiple variable assignment and search

Status
Not open for further replies.

darkmuse

ISP
Aug 2, 2002
12
US
I have this current script, that a few of you helped me get working a few weeks ago. Thanks everybody. Now I need to modify it to be a little more flexible.



BEGIN {
num = 0
while ((getline < &quot;dc.users&quot;) > 0) {
nam[num] = $0
num++
}
}
{
for (j=0;j<num;j++) {
if ($1 ~ &quot;^&quot;nam[j]&quot;$&quot;) {
$0 = substr($0, 1, length($0)-1) &quot;-disconnected &quot; date &quot;\&quot;&quot;;
flg[j] = 1
}
}
print
}
END {
for (j=0;j<num;j++) if (!flg[j]) print nam[j] > &quot;dc.users.notfound&quot;
}




What I need for the script to do is assign a 2nd varible like:

var_other = $1+&quot;@domain.com&quot;

Assuming $1 is bob
var_other should be bob@domain.com

Not sure about the about syntax or where it needs to go in the script.

Then I need the script to use the if statement to search for either variable then continue on with the script.. like:

if ($1 ~ &quot;^&quot;nam[j]&quot;$&quot; || var_other ~ &quot;^&quot;nam[j]&quot;$&quot;)

Again, not sure about the syntax of above.

Any help is appreciated,
Randall
 
something like that?

BEGIN {
myDomain=&quot;@domain.com&quot;
num = 0
while ((getline < &quot;dc.users&quot;) > 0) {
nam[num] = $0
num++
}
}
{
ATdomain=$1 ATdomain
for (j=0;j<num;j++) {
if ( ($1 ~ &quot;^&quot;nam[j]&quot;$&quot;) || (ATdomain ~ &quot;^&quot;nam[j]&quot;$&quot;) ){
$0 = substr($0, 1, length($0)-1) &quot;-disconnected &quot; date &quot;\&quot;&quot;;
flg[j] = 1
}
}
print
}
END {
for (j=0;j<num;j++) if (!flg[j]) print nam[j] > &quot;dc.users.notfound&quot;
} vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Yep, that looks about like what i'm looking for..

One question though.

Are the variables right in the modified script above? I would think it should look like this... Maybe i'm wrong.

...

myDomain=&quot;@domain.com&quot;

...

ATdomain=$1 myDomain
for (j=0;j<num;j++) {
if ( ($1 ~ &quot;^&quot;nam[j]&quot;$&quot;) || (ATdomain ~ &quot;^&quot;nam[j]&quot;$&quot;) )

...
 
ooops - I'm baaad^N

sorry 'bout that - you're right - my typo vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I made the changes to the script. It now looks like this:



BEGIN {
myDomain=&quot;@mounet.com&quot;
num = 0
while ((getline < &quot;dc.users&quot;) > 0) {
nam[num] = $0
num++
}
}
{
ATdomain=$1 myDomain
for (j=0;j<num;j++) {
if ( ($1 ~ &quot;^&quot;nam[j]&quot;$&quot;) || (ATdomain ~ &quot;^&quot;nam[j]&quot;$&quot;) ){
$0 = substr($0, 1, length($0)-1) &quot;-disconnected &quot; date &quot;\&quot;&quot;;
flg[j] = 1
}
}
print
}
END {
for (j=0;j<num;j++) if (!flg[j]) print nam[j] > &quot;dc.users.notfound&quot;
}


For some reason, it didn't change the user.new file like before.

I execute the script with this command line:

awk -f automate.awk.script date=`date +%m-%d-%y` users > users.new

Let me give you a little background.


I have a file dc.users that contains:

bob
steve
mark

and I have a users file that contains:

joe Password=&quot;xxxxx&quot;
steve Password=&quot;xxxxx&quot;
mark@mounet.com Password=&quot;xxxxx&quot;

After I execute the script, the users.new file should look like:

joe Password=&quot;xxxxx&quot;
steve Password=&quot;xxxxx-disconnected&quot;
mark@mounet.com Password=&quot;xxxxx-disconnected&quot;



For some reason users.new looks like this:

joe Password=&quot;xxxxx&quot;
steve Password=&quot;xxxxx-disconnected&quot;
mark@mounet.com Password=&quot;xxxxx&quot;

 
I think you've confused what's in &quot;users&quot; and what's in &quot;dc.users&quot;.

I think I've made the right assumptions:

BEGIN {
myDomain=&quot;@mounet.com&quot;
num = 0
while ((getline < &quot;dc.users&quot;) > 0) {
nam[$1] = $0
num++
}
}

{
noDomain=substr($1,1,index($1,&quot;@&quot;)-1);
if ( ($1 in nam ) || (noDomain in nam) ) {
$0 = substr($0, 1, length($0)-1) &quot;-disconnected &quot; date &quot;\&quot;&quot;;
flg[j] = 1
}
}
1

END {
for (j=0;j<num;j++) if (!flg[j]) print nam[j] > &quot;dc.users.notfound&quot;
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I'll try the above in a test run...

But to answer you question about what info goes in what file...

The dc.user file only has the names of customers in the user file that need to be disconnected.

dc.users :

joe
steve
mark

The users file is the actual radius file that our customers authenticate from.

users :

joe Password=&quot;xxxxx&quot;
steve Password=&quot;xxxxx&quot;
mark@mounet.com Password=&quot;xxxxx&quot;

If the name in the dc.users file exists, then the same name needs to be modified in the users file.

The name in the users file may be just the name itself or it may have @mounet.com on the end of it.
 
Well, I tried it, and it will insert the -disconnect 09-19-02 for the correct username, but it isn't updating the dc.users.notfound correctly.

Try it using these files:

dc.users:

bob
steve
mark

users:

tom Password = &quot;xxxxx&quot;
steve Password = &quot;xxxxx&quot;
mark@mounet.com Password = &quot;xxxxx&quot;


the script itself is in a file called:
automate.awk.script

The command I execute is:

awk -f automate.awk.script date=`date +%m-%d-%y` users > users.new

After the script has finished, the users.new file should look like this:

users.new:

tom Password=&quot;xxxxx&quot;
steve Password=&quot;xxxxx-disconnected&quot;
mark@mounet.com Password=&quot;xxxxx-disconnected&quot;

The dc.users.notfound file should be:

dc.users.notfound :

bob



I hope this helps. By the way, thanks for all the help.

Randall
 
not optimized - don't have time *now*

BEGIN {
myDomain=&quot;@mounet.com&quot;
num = 0
while ((getline < &quot;dc.users&quot;) > 0) {
nam[$1] = $0
num++
}
}

{

flg[$1]++;
noDomain=substr($1,1,index($1,&quot;@&quot;)-1);
if ( ($1 in nam ) || (noDomain in nam) ) {
$0 = substr($0, 1, length($0)-1) &quot;-disconnected &quot; date &quot;\&quot;&quot;;
}
}
1

END {
for (i in nam ) {
fqdName=i myDomain
if (!(i in flg) && !(fqdName in flg)) print i >> &quot;dc.users.notfound&quot;
}
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I tried this last one and it seems to work fine.

Thanks for all the help.

Randall
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top