LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-14-2006, 09:38 AM   #1
tonedeaf1969
LQ Newbie
 
Registered: Sep 2005
Posts: 8

Rep: Reputation: 0
awk: loop through ldap aliases


Hi All:

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.
 
Old 11-14-2006, 09:53 AM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
Could you post a sample of your data?
 
Old 11-14-2006, 11:33 AM   #3
tonedeaf1969
LQ Newbie
 
Registered: Sep 2005
Posts: 8

Original Poster
Rep: Reputation: 0
Here is my record:

UserID,DisplayName,Alias,Alias,Alias,Alias,Alias
kmmurphy,Kevin Murphy,k.michael.murphy,k.michael.murphy,k.michael.murphy,k.michael.murphy,k.michael.murphy


I use two aliases (k.michael.murphy & kevin.murphy).

FYI: this will run on a SLOX 4.1 mail server w/ BASH

Thanks.

Last edited by tonedeaf1969; 11-14-2006 at 11:42 AM.
 
Old 11-14-2006, 11:52 AM   #4
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
I was looking for an example of your input data (i.e. 3 or 4 records from your cvs file). The above data looks like your output results.
 
Old 11-14-2006, 01:16 PM   #5
tonedeaf1969
LQ Newbie
 
Registered: Sep 2005
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by fordeck
I was looking for an example of your input data (i.e. 3 or 4 records from your cvs file). The above data looks like your output results.
OOPS, sorry about that. Here's my ldap ldif entry (I'm using the ldif dump as my data file). Here's the command I'm running.

awk -f test.awk file.ldif > output.csv

dn: uid=kmmurphy,dc=xxxxxxx,dc=xx,dc=xxxx
comFireGroupID: 501
comFireGroupID: 515
comFireGroupID: 516
homeDirectory: /home/kmmurphy
loginShell: /bin/bash
uidNumber: 503
shadowMax: 99999
shadowMin: 0
shadowWarning: 7
shadowInactive: 0
mailenabled: ok
comFireAppointmentDays: 5
comFireTaskDays: 5
objectClass: top
objectClass: shadowAccount
objectClass: posixAccount
objectClass: person
objectClass: inetOrgPerson
objectClass: SuSEeMailObject
objectClass: SuSEcomFireConfig
objectClass: sambaAccount
objectClass: SuSEVirtUserObject
structuralObjectClass: inetOrgPerson
entryUUID: f87e4246-51e8-102a-86ad-ba8d04e51abc
creatorsName: uid=cyrus,dc=xxxxxxx,dc=xx,dc=xxxx
createTimestamp: 20060327142314Z
vaddress: paging@xxxxxxxxx.xxx
vaddress: staff@xxxxxxxxx.xxx
vaddress: support@xxxxxxxxx.xxx
vaddress: it@xxxxxxxxx.xxx
vaddress: log@xxxxxxx.xxx
loginDestination: groupware
gidNumber: 501
DyCEenabled: FALSE
SuSETimeZone: America/Halifax
givenName: Kevin
preferredLanguage: EN
c: CA
sn: Murphy
alias: Kevin.Murphy
alias: k.michael.murphy
o: xxxxxxx xxxxxx xxxxx
userPassword:: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
shadowLastChange: 13381
uid: kmmurphy
pwdLastSet: 1156170155
logonTime: 0
logoffTime: 2147483647
kickoffTime: 2147483647
pwdCanChange: 0
pwdMustChange: 2147483647
displayName: Kevin Murphy
cn: Kevin Murphy
rid: 2006
primaryGroupID: 2003
lmPassword: xxxxxxxxxxxxxxxxxxxxxxx
ntPassword: xxxxxxxxxxxxxxxxxxxxxx
acctFlags: [U ]
mail: Kevin.Murphy@xxxxxxxxx.xxx
OpenLDAPaci: 1#entry#grant;r,w,s,c;cn,initials,title,l,birthday,description,st
reet,postalcode,st,c,susetimezone,homephone,mobile,pager,facsimiletelephonenu
mber,telephonenumber,sn,givenname;r,s,c;[all]#self#
entryCSN: 2006110618:36:10Z#0x0001#0#0000
modifiersName: uid=cyrus,dc=xxxxxxx,dc=xx,dc=xxxx
modifyTimestamp: 20061106183610Z

Last edited by tonedeaf1969; 11-14-2006 at 01:18 PM.
 
Old 11-16-2006, 07:52 PM   #6
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi,

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()
}
 
Old 11-17-2006, 11:56 AM   #7
tonedeaf1969
LQ Newbie
 
Registered: Sep 2005
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by osvaldomarques
Hi,

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).
 
  


Reply

Tags
alias, awk, ldap, loops


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk "for loop" question rharvey@cox Programming 5 05-03-2006 06:12 AM
sendmail and NIS databases (aliases, mail.aliases) - what kind of databases? cotton213 Linux - Software 0 03-14-2006 05:57 PM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM
awk - simple 'for' loop doing my nut... davee Programming 2 06-30-2004 08:54 AM


All times are GMT -5. The time now is 02:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration