LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-2007, 10:08 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
shell script help for users


Hello guys, I would appreciate if someone can help me for this task:

I have a large txt file that contains lots of user ids in different format. I need to run this against a database output ldif file and print to a file whether they exist or not. And if they exist then I need to print their information from the ldif file. I appreciate in advance if anyone can assist.

THe user file looks like ( a chunk of it ):

060606ab
12345678910
110maryland
123thebestfather
1superman1
22221111333_1234567

And the ldif file ( a section of it ) looks like:

dn: uid=MYFILE1,ou=someou,o=test.com
21tCGX2RdpxnkaJU0mI/BC0jdaBBbUt8gjnRxAhMU2
authpassword;orclsomepwd: {ABC}a2F4=
businesscategory: SomeCategory
createtimestamp: 2007070911234z
creatorsname: cn=someload
modifiersname: cn=someoad
modifytimestamp: 20070709181203z
objectclass: top
objectclass: someUser
orclguid: ABCDFF4AD107
orclnormdn: uid=myid1,ou=someconsumer,o=vz.com
pwdchangedtime: 20070709181203z
uid: MYFILE1
userpassword: ABCDRFGEmBXrHCKZa2F4=
canswer: Hello
cinkid: MYFILE1
cquestion: What is my mother's maiden name
cusergroup: somecustomer
cuserstatus: 0


Actually if a uid in the txt file exist in the ldif file, I want to print dn, cquestion, and userstatus

server information: sun os 5.9
it has perl version 5.6.1

any script/program such as shell script, perl script using awk/sed/etc would help me a lot, I appreciate ...
 
Old 11-14-2007, 10:48 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.3
Posts: 17,537

Rep: Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420
This would be easy in Perl:

I'd go with building a hash to store the ldif info, but each entry would only contain the uid, dn, cquestion, and userstatus, keyed on uid.
Then read through the user file in a loop and do
Code:
if( exists( $ldif{$uid} ) )
{
    print "$uid $ldif{'dn'} $ldi{'cq'} $ldif{'status'}\n";
}
how well do you know Perl.?
 
Old 11-15-2007, 03:02 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,431

Rep: Reputation: 203Reputation: 203Reputation: 203
need more clues,
the users are each on a line, like 123thebestfather, 1superman1 etc?

is the uid in idfile uid:? (MYFILE1)
a more complete ldif file would be something to work with.

do we get paid
 
Old 11-15-2007, 04:14 AM   #4
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bigearsbilly View Post
need more clues,
the users are each on a line, like 123thebestfather, 1superman1 etc?

is the uid in idfile uid:? (MYFILE1)
a more complete ldif file would be something to work with.

do we get paid


Hi - Yes uid's are like you said but there are many of them in a text file. The questions is that that text file contains uid's are in the ldif file or not... For exaple, if one of the uid is found, then print the values such as dn. if not found, print elsewhere etc. Thank you very much.
 
Old 11-15-2007, 05:28 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,431

Rep: Reputation: 203Reputation: 203Reputation: 203
we don't mind helping, but we'd want some more data to play with,
a ldif file with a few users entries would be nice.
 
Old 11-15-2007, 08:10 AM   #6
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Hi, please see the sample ldif file with 3 entries in it...in real server it is like 15GB. Thank you very much again...

dn: uid=test1,ou=someconsumer,o=abc.com
canswer: 111111111
cuserstatus: 0
uid: test1
businesscategory: SomeConsumer
objectclass: top
objectclass: SomeUser
clinkid: test1
userpassword: password1
cusergroup: regularcustomer
cquestion: what is your father's name?

dn: uid=test2,ou=someconsumer,o=abc.com
canswer: 22222222
cuserstatus: 1
uid: test1
businesscategory: SomeConsumer
objectclass: top
objectclass: SomeUser
clinkid: test1
userpassword: password1
cusergroup: regularcustomer
cquestion: In what City did/do your parents live?

dn: uid=test3,ou=someconsumer,o=abc.com
canswer: 33333333
cuserstatus: 2
uid: test1
businesscategory: SomeConsumer
objectclass: top
objectclass: SomeUser
clinkid: test1
userpassword: password1
cusergroup: regularcustomer
cquestion: In what City did/do your parents live?
 
Old 11-15-2007, 08:33 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,431

Rep: Reputation: 203Reputation: 203Reputation: 203
FIFTEEN Gig !!!!!!

well we won't bother slurping that file into a big hash then
(agreed chrism?)
 
Old 11-15-2007, 08:36 AM   #8
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
it is s big db...i meant 15 MB apoligies.... typed it wrong...
 
Old 11-15-2007, 08:49 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,431

Rep: Reputation: 203Reputation: 203Reputation: 203
phew,
well, you could slurp that away maybe.

If I were you i would consider using at minimum a DBM file to store it all,
then you could easily look it up.

does anything else use this file?
how many searches do you make,
1 a day?
200 a minute?
 
Old 11-15-2007, 09:42 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,431

Rep: Reputation: 203Reputation: 203Reputation: 203
Here's a quick and dirty go,
not fully robust
multiple fields like objectclass:
will get mashed together in the wash

does regex match (if you prefer), and will match multiple names on the command line

Code:
#!/usr/bin/perl -s

my $db_name = "database";  # change this bit

local $/ = "";          # paragraph mode
local $\ = "\n";        # print terminator

open DATABASE, "<$db_name" or die "cannot open $db_name:$!\n";
@ARGV or die "search for who?\n";;

while (<DATABASE>) {

    $debug and print "=" x 30, "\n";    # -debug switch

    @L = split /(\w+):\s*/;     # split on colon fields
    shift @L;                   # first field is null, lose it
    chomp @L;

    $debug and print "@L";

    $H = { @L };                # convert to a hash


    foreach $uid (@ARGV) {

        # UNCOMMENT TO ALLOW AN EXACT MATCH ONLY
        # next unless $H->{uid} eq $uid;

        # this is a regex match (not shell patterns)
        next unless $H->{uid} =+ m/$uid/;

        # choose your fields
        print "=" x 30;
        print $H->{dn};
        print $H->{cquestion};
        print $H->{userstatus};
    }

}
Code:
./1.pl ROG test3
==============================
uid=ROGER,ou=someconsumer,o=abc.com
what is your father's name?

==============================
uid=test3,ou=someconsumer,o=abc.com
In what City did/do your parents live?
 
Old 11-15-2007, 10:23 AM   #11
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Would you please clear something for me?

How could I store txt and ldif in a dbm file ? The need is to look for uids given in txt in the ldif file, then if they exist print the info like dn etc...

I tried modifying the line "my $db_name = "test.txt"; # change this bit" however I couldnt get it running yet. I have 2 files whioh one contains uid's list, and the other is the ldif file. Would you please tell me where to modify this perl script in order to get it running? Thank you very much.

Last edited by cmontr; 11-15-2007 at 10:42 AM.
 
Old 11-15-2007, 05:09 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.3
Posts: 17,537

Rep: Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420Reputation: 2420
Actually BB, I meant read the ldif file line by line as normal, and (as I said) ONLY store the relevant fields, which would cut down the amt of data dramatically. Even a 15 GB file would prob shrink enough. Could always use a Tied hash if it's too big.

BTW:
next unless $H->{uid} =+ m/$uid/;
shouldn't that be
next unless $H->{uid} =~ m/$uid/;
you did say regex

Last edited by chrism01; 11-15-2007 at 05:12 PM.
 
  


Reply


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
Shell Script to add samba users dickohead Programming 14 11-18-2016 02:28 AM
UNIX : Switching users in a shell script? aravindts Programming 5 09-07-2012 03:24 AM
Adding new users via Shell script coolfrog Linux - General 10 12-05-2010 10:47 AM
Setting password for users in a shell script?? krishvij Linux - Newbie 3 03-10-2005 09:17 AM
Shell Script: Add Users from file flobadon Programming 3 12-07-2004 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:25 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration