The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Help with Perl Scripting Issue. manik112 Shell Programming and Scripting 23 12-13-2008 12:52 PM
Perl Script Issue - Please Help * Thanks!!! jroberson Shell Programming and Scripting 8 11-03-2008 03:47 AM
perl issue .. zedex Shell Programming and Scripting 3 09-13-2008 11:22 PM
issue with if loop in perl amitrajvarma Shell Programming and Scripting 4 01-09-2008 12:02 AM
Perl problem (compiling issue) 01000101 Shell Programming and Scripting 3 05-24-2006 10:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 01-27-2009
raj001 raj001 is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 13
Perl Issue

Hi,

I got this script from the web, this generates an LDAP report in CSV format.

Code:
#!/usr/bin/perl
#
# Copyright (c) 2004
# Ali Onur Cinar &060;cinar&064;zdo.com&062;
#
# License:
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for non-commercial purposes and without fee is hereby
# granted, provided that the above copyright notice appear in all copies
# and that both the copyright notice and this permission notice and
# warranty disclaimer appear in supporting documentation, and that the name
# of Ali Onur Cinar not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior permission.
#
# @author A. Onur Cinar <cinar(a)zdo.com># @version $Id: ldap2csv.pl,v 1.1 2004/12/07 15:12:38 cinar Exp $#
use Net::LDAP;
@fields = # fields to export
(  # Label Field Name Index 
  ['First Name', 'givenName' , 0], ['Last Name' , 'sn' , 0], ['Title' , 'title' , 0],
  ['Email' , 'mail' , 0], ['Email 2' , 'mail' , 1], ['Phone' , 'telephoneNumber', 0], 
  ['Phone' , 'telephoneNumber', 1], ['Phone' , 'telephoneNumber', 2], 
  ['Phone' , 'telephoneNumber', 3], ['Phone' , 'telephoneNumber', 4], 
  ['Mobile' , 'mobile' , 0], 
  ['Birthday' , 'birthday' , 0], ['Address' , 'street' , 0], ['Address' , 'street' , 1], 
  ['Address' , 'street' , 2],
);
 
$ldap = Net::LDAP->new('localhost') or die "$@"; # open connection
$mesg = $ldap->bind( # login 
            "uid=test", password => "password");
$mesg = $ldap->search( # search 
            base => "dc=abc", 
            filter => "(objectClass=inetOrgPerson)"
);
 
$DATETIME=`date`;
 
sub get # get field value
{ return $_[0]->exists($_[1]) ? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0] : '';}
 
print "$DATETIME"; # first line with file created date/time
print '"'.(join '","', "Report", # header map {$_->[0]} @fields)."\"\n";
foreach $entry ($mesg->all_entries) # for each entry
{ 
   @dn = map {s/[a-z]+=//gi; $_ = ucfirst} # organization 
          reverse split /,/, $entry->dn; 
   shift @dn; shift @dn; 
   shift @dn; pop @dn; 
 
   print '"'
       . (join '","', "@dn",         # row map   
            {get $entry, $_->[1], $_->[2]} @fields)
       . "\"\n";
}
$mesg = $ldap->unbind; # close connection
This script prints value in the following format :

Code:
 
"abc","1234","HCM","John D","JOHN D","rocks" 12","seigo"
"abc2","12345","TDM","Mr Say","Mr Say T","hupt","margo"
If you see there is a quote in the field (rocks" 12), I want to remove the quote from the field value only.Is there any way to modify the script and remove the quotes occuring in any of the field?

Thanks in Advance.

Last edited by otheus; 01-28-2009 at 06:31 AM.. Reason: formatting
  #2 (permalink)  
Old 01-27-2009
sharadpisal sharadpisal is offline
Registered User
  
 

Join Date: May 2008
Posts: 28
Change
_______________________
shift @dn; pop @dn;

print '"'.(join '","', "@dn", # row map
_______________________

To

_______________________
shift @dn; pop @dn;
map {$_ =~ s/"//} @dn;
print '"'.(join '","', "@dn", # row map
_______________________
  #3 (permalink)  
Old 01-27-2009
raj001 raj001 is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 13
Sorry,

The perl script got distorted while posting, here is snapshot of the exact line:

foreach $entry ($mesg->all_entries) # for each entry
{
@dn = map {s/[a-z]+=//gi; $_ = ucfirst} reverse split /,/, $entry->dn; # organization
shift @dn;
pop @dn;
print '"'.(join '","', "@dn", map {get $entry, $_->[1], $_->[2]} @fields)."\"\n"; # row

}

Please be cautious that "get" is actually a subroutine as mention is script:

sub get # get field value
{
return $_[0]->exists($_[1])
? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0]
: '';
}

If still it is not clear, here is the link to access the script, from where I have downloaded the script:

ZDO.COM - Articles - Export Data From LDAP As CSV

Thanks,
Raj
  #4 (permalink)  
Old 01-27-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
@dn = map {s/[a-z]+=//gi; tr/"//d; $_ = ucfirst} # organization
  #5 (permalink)  
Old 01-27-2009
raj001 raj001 is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 13
I really appreciate your effort Kevin.

The following commad actually prints header, cant we use the same thing on the below line which prints data field values;

print '"'.(join '","', "@dn",map {get $entry, $_->[1], $_->[2]} @fields)."\"\n";
  #6 (permalink)  
Old 01-27-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
maybe:

Code:
tr/"//d for @fields;
print '"'.(join '","', "@dn",map {get $entry, $_->[1], $_->[2]} @fields)."\"\n";
  #7 (permalink)  
Old 01-27-2009
raj001 raj001 is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 13
This is not working, but may be we are coming closer.

actually the fields are printed out from "$entry" variable, so can't we put something around the variable or into the subroutine "get".


print '"'.(join '","', "@dn", map {get $entry, $_->[1], $_->[2]} @fields)."\"\n"; # row

# subroutine

sub get
{
return $_[0]->exists($_[1])
? $_[0]->get($_[1])->[$_[2] ? $_[2] : 0]
: '';
}
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0