Solved

Perl - sort and print phone numbers

Posted on 2013-01-06
5
484 Views
Last Modified: 2013-01-07
I have a file with user name, telephone numbers and the addn for each user where each is separated by a blank line such as:

________________________________________________________________________________
dn: cn=John Doe,ou=employee,o=abc,c=AN
telephonenumber: +1 432 111 2222
addn: CN=John Doe,OU=Users,DC=xxx,DC=abc,DC=com

dn: cn=Jane Doe,ou=employee,o=abc,c=AN
telephonenumber: +44 1293 112233
telephonenumber: +44 1293 112244 (SINet)
telephonenumber: +44 1293 112244
addn: CN=Jane Doe,OU=Users,DC=xxx,DC=abc,DC=com

dn: cn=IPT-GB0000-DP2,ou=role,O=abc,C=AN
telephonenumber: +44 1293 555555
telephonenumber: +44 1293 333333
telephonenumber: +44 1293 444444
addn: CN=IPT-GB0000-DP2,CN=Users,DC=xxx,DC=slb,DC=com

dn: cn=IPT-GB8888-DP3,ou=role,O=abc,C=AN
telephonenumber: +44 1293 777777
telephonenumber: +44 1293 999999 (SINet)
telephonenumber: +44 1293 000000
addn: CN=IPT-GB8888-DP3,CN=Users,DC=xxx,DC=abc,DC=com
________________________________________________________________________________

for each group in the file I need to print out the:

addn:
telephonenumber:


The problem I have is that I need to only print out one telephone number for each user.
If the user has multiple phone numbers then I need to take the line that contains (SINet) to use for the telephone number and if it does not contain (SINet) in any of the telephone numbers then I need to use the first line of the telephone numbers for that user.


So for these 4 examples I would get output such as:

addn: CN=John Doe,OU=Users,DC=xxx,DC=abc,DC=com
telephonenumber: +1 432 111 2222

addn: CN=Jane Doe,OU=Users,DC=xxx,DC=abc,DC=com
telephonenumber: +44 1293 112244 (SINet)

addn: CN=IPT-GB0000-DP2,CN=Users,DC=xxx,DC=slb,DC=com
telephonenumber: +44 1293 555555

addn: CN=IPT-GB8888-DP3,CN=Users,DC=xxx,DC=abc,DC=com
telephonenumber: +44 1293 999999 (SINet)


The script I made is printing out the addn and phone number but always ends up with the first phone number where if there is a phone number with a tag of (SINet) I need to use that one, if not then the first line is needed.

Thanks for any help on this!!
0
Comment
Question by:bt707
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 38749251
$/="";
while( <DATA> ){
    print /\b(addn:.*\n)/,(/(telephonenumber:.*\(SINet\).*\n)/)[0]||/(telephonenumber:.*\n)/;
}
__DATA__
dn: cn=John Doe,ou=employee,o=abc,c=AN
telephonenumber: +1 432 111 2222
addn: CN=John Doe,OU=Users,DC=xxx,DC=abc,DC=com

dn: cn=Jane Doe,ou=employee,o=abc,c=AN
telephonenumber: +44 1293 112233
telephonenumber: +44 1293 112244 (SINet)
telephonenumber: +44 1293 112244
addn: CN=Jane Doe,OU=Users,DC=xxx,DC=abc,DC=com

dn: cn=IPT-GB0000-DP2,ou=role,O=abc,C=AN
telephonenumber: +44 1293 555555
telephonenumber: +44 1293 333333
telephonenumber: +44 1293 444444
addn: CN=IPT-GB0000-DP2,CN=Users,DC=xxx,DC=slb,DC=com

dn: cn=IPT-GB8888-DP3,ou=role,O=abc,C=AN
telephonenumber: +44 1293 777777
telephonenumber: +44 1293 999999 (SINet)
telephonenumber: +44 1293 000000
addn: CN=IPT-GB8888-DP3,CN=Users,DC=xxx,DC=abc,DC=com
0
 

Author Closing Comment

by:bt707
ID: 38749279
Thanks ozo,

That sure works for that, still have a numbers of things to work out but that is a big help on that part.

Thanks!!!
0
 

Author Comment

by:bt707
ID: 38749318
ozo,

One other question on that, it works great but I need to put the telephonenumber and the addn into a variable as I need to do several other things to it, but from the print you have I cannot get that right.

How can I store the addn and telephonenumber to a variable rather than just printing it?

Thanks,
0
 
LVL 84

Expert Comment

by:ozo
ID: 38750517
($addn)=/\b(addn:.*)/;
($telephonenumber)=(/(telephonenumber:.*\(SINet\).*)/)[0]||/(telephonenumber:.*)/;
0
 

Author Comment

by:bt707
ID: 38750525
Thanks ozo, I found a slightly different way to do it but that looks even better.

Thanks!!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl script 4 94
instmodsh - listing perl modules 2 63
Any syntax error for this clone.plscript 6 125
Writing a parser for java language 4 61
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now