ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hope you can help out a scripting newbie here. A forum search hasn't given me what I'm looking for. I need to fish out all the alias entries in our LDAP database and match them to the proper user names. I've decided to output a comma delimited file.
If a user has one alias, no problem, that much I've got figured out. It's the users with multiple aliases that have got me stumped. I tried this ham fisted approach(I'm guessing no one has more than 5 aliases):
BEGIN {
UserID = ""
DisplayName = ""
Alias = ""
printf(" UserID,DisplayName,Alias,Alias,Alias,Alias,Alias\n");
}
/^uid: / {UserID=$2}
/^displayName: / {DisplayName=$2" " $3}
/^alias: / {Alias=$2}
/^dn: / {
if(UserID != "") printf("%s,%s,%s,%s,%s,%s,%s\n",UserID,DisplayName,Alias,Alias,Alias,Alias,Alias)
UserID = ""
DisplayName = ""
Alias = ""
Alias = ""
Alias = ""
Alias = ""
Alias = ""
}
END {
printf("%s,%s,%s,%s,%s,%s,%s\n",UserID,DisplayName,Alias,Alias,Alias,Alias,Alias);
}
This prints 5 instances of the first alias, not what I'm looking for. I'm thinking a loop of some kind is what I'm looking for, but I'm not sure how/where to put it. Any pointers would be appreciated. thanks.
Last edited by tonedeaf1969; 11-22-2006 at 01:39 PM.
You have to put the aliases into an array. When it's time to print, you have to print each array element; I got your source and modified it as an example:
Code:
function print_user()
{
if(UserID != "")
{
printf("%s", UserID)
for (i = 1; i <= ind; i++)
printf(",%s", Alias[i])
printf("\n")
}
}
BEGIN {
UserID = ""
DisplayName = ""
Alias = ""
printf(" UserID,DisplayName,Alias,Alias,Alias,Alias,Alias\n");
}
/^uid: / {UserID=$2}
/^displayName: / {DisplayName=$2" " $3}
/^alias: / {Alias[++ind]=$2}
/^dn: / {
print_user()
UserID = ""
DisplayName = ""
ind = 0
}
END {
print_user()
}
You have to put the aliases into an array. When it's time to print, you have to print each array element; I got your source and modified it as an example:
Code:
function print_user()
{
if(UserID != "")
{
printf("%s", UserID)
for (i = 1; i <= ind; i++)
printf(",%s", Alias[i])
printf("\n")
}
}
BEGIN {
UserID = ""
DisplayName = ""
Alias = ""<======I had to remove this.
printf(" UserID,DisplayName,Alias,Alias,Alias,Alias,Alias\n");
}
/^uid: / {UserID=$2}
/^displayName: / {DisplayName=$2" " $3}
/^alias: / {Alias[++ind]=$2}
/^dn: / {
print_user()
UserID = ""
DisplayName = ""
ind = 0
}
END {
print_user()
}
Thanks a lot, that worked fine!
One quick note, I had to remove the "Alias" variable. The script was throwing scalar context errors(see note above).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.