LDAP data in CSV format - Part II


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting LDAP data in CSV format - Part II
# 1  
Old 05-17-2016
LDAP data in CSV format - Part II

User Aia had created this below script to help translate LDIF files to CSV format, and it works very well (thanks again Aia if you are reading this)

Here is the original thread.
LDAP data in CSV format

However I've encountered a scenario where it may need to be changed slightly to be able to apply it. It was created for LDAP dir. user objects but I also have a need to translate an LDIF file containing group objects and their attributes (group members) into CSV files.

For the "group" objects in our LDAP directory, the group's members are expressed by a series of 'uniquemember' attributes, one per user. These attributes all have an identical name, and there are varying amounts of them depending on the group (we have ~20 groups). Some have 7-8 "uniquemember"s listed, some have hundreds.

For instance:

Code:
cn=Director,cn=groups,DC=MARCHON,DC=COM
cn=Director
uniquemember=uid=skressman,cn=users,DC=MARCHON,DC=COM
uniquemember=uid=rf1,cn=users,DC=MARCHON,DC=COM
uniquemember=uid=dingino,cn=users,dc=marchon,dc=com
uniquemember=uid=lgittler,cn=users,dc=marchon,dc=com
uniquemember=uid=junger,cn=users,dc=marchon,dc=com
uniquemember=uid=lbuchanan,cn=users,DC=MARCHON,DC=COM

The script lists statically the of attribute values separated by commas, so it's not capable of reading the varying amounts of 'uniquemember' attributes (the size of each group is different). I am trying to figure out how to express these groups and their members using the same CSV format or something very close.


Original script created by Aia below:

Save as ldap2csv.pl
Run as perl ldap2csv.pl ldapfile


Code:
#!/usr/bin/env perl
use strict;
use warnings;

# Bail out if no file is given as an argument.
my $ldap_file = shift || usage();

# To save output with extension .csv.
my $csv_file = "$ldap_file.csv";

# Preserve order of header labels.
my @header = qw(uid0 givenname salesorg sn telephonenumber l employeetype mail
                    uid c postalcode cn preferredlanguage employeenumber st);

# Boiler plate to open input and output files handles.
open my $in, '<', $ldap_file or die;
open my $out, '>', $csv_file or die;

# Write the header labels.
print  $out "uid,", join (',', @header[1..$#header]), "\n";

# Work engine.
{
    # Record separator is a paragraph representation.
    local $/="\n\n";

    # Process one record at a time.
    while(my $record = <$in>) {
        chomp $record; # Remove the record separator.
        my %data;
        @data{@header} = ();  # To label the data.
        my @entries = split '\n', $record; # Create data entries.

        # Save first entry uid or it will be over written by second instance.
        $data{'uid0'} = qq{"$entries[0]"};

        # Work with each entry.
        for my $entry (@entries){
            my ($key, $value) = split "=", $entry, 2; # split only by first equal.
            $value =~ s/\s+$//; # Clean trailing spaces.
            $value = qq{"$value"} if $value =~ /,/; # Surround with double quotes any entries with comas.
            $data{$key} = $value;
        }
        # Separate entries with comas.
        my $row = join ',', (map{ $data{$_}?$data{$_}:""} @header);

        # Write to output file.
        print $out "$row\n";
    }
}
# Dismiss file handles.
close $in;
close $out;

# Feed back to user.
print "$csv_file has been saved in the current directory\n";

sub usage {
    print "Usage: $0 ldapfilename\n";
    exit 1;
}

Code:

# 2  
Old 05-28-2016
LDAP data in CSV format in KSH

Here's a ksh script accomplishing the same end result with the exception of including all members in each group, instead of only the last member. The script also archives the csv file before clearing the contents. Is this what you are looking for?
Smilie

Code:
#!/usr/bin/ksh93

if [[ $# -eq 0 ]];then
  print "Invalid Usage: Argument expected."
  exit 1
fi

LDAP_CSV_FILE="./SAM_GROUPS.ldif.csv"
LDIF_FILE="./"$@

if [[ ! -f ${LDIF_FILE} ]] ;
then
  print "SAM ldif file called" $@ "does not exist."
  exit 1
fi

if [[ -f ${LDAP_CSV_FILE} ]] ;
then
  cp ${LDAP_CSV_FILE} ${LDAP_CSV_FILE}"."`date +%Y%m%d`
 if [ $? == 0 ]; then
    cat /dev/null > ${LDAP_CSV_FILE}
 fi
fi

 while read LINE
do
if [[ ${LINE} == cn=* ]] && [[ ${LINE} != *","* ]] ;
then
  GROUP=${LINE:3}
fi

if [[ ${LINE} == uniquemember=* ]] ;
then
  MEMBER='"'${LINE:13}'"'
fi

if [[ ${LINE} == *uniquemember=* ]] ;
then
  echo ${GROUP}","${MEMBER} >> ${LDAP_CSV_FILE}
fi

done < ${LDIF_FILE}

This User Gave Thanks to ItalianTripod For This Post:
# 3  
Old 06-01-2016
Three thumbs up for the Italian Tripod. Godspeed, kind stranger.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Log file data into CSV format

I m looking for help here!!! Can we filter the below log data into CSV format ? 1 2 3 4 5 6 7 8 timestamp INFO <text > - Some text (1 Reply)
Discussion started by: MohSalNiz
1 Replies

2. Shell Programming and Scripting

Can we filter the below log data into CSV format?

HI , I m looking for help here!!! Can we filter the below log data into CSV format ? timestamp INFO <text > - Some text Drive .. Need a format of separate field such as 1 2 3 4 ... (2 Replies)
Discussion started by: MohSalNiz
2 Replies

3. Shell Programming and Scripting

Convert csv data to html format

I am new to html and need to convert the attached csv file data to html format ; running into issues. please assist. #!/bin/ksh echo "<html>" ; echo "<head><style> table {border-collapse: collapse;} table, td, th {border: 1px solid black;} </style></head>" echo "<title> REPORT </title>" echo... (0 Replies)
Discussion started by: archana25
0 Replies

4. Shell Programming and Scripting

LDAP data in CSV format

Hi all, I am new here, please don't eat me alive.. I am trying to find a good community to learn and participate in unix / linux discussions to help me improve in my current job. That being said, I have a problem which I didn't expect to challenge me, but I can't seem to find a viable... (8 Replies)
Discussion started by: tfm217
8 Replies

5. Shell Programming and Scripting

CSV data format manipulation

Hi There I need a script which will pick up the data from a .CSV file and reformat it as per the requirement and write it to another .CSV file. I am using an application that will only take data in a particular format and need something that will convert without manual intervention. The... (4 Replies)
Discussion started by: rbggbr16
4 Replies

6. Shell Programming and Scripting

Conversion of xhtml data into csv format using dump utility

Hi Unix Gurus, I tried to convert the attached xhtml table content into csv file using unix shell script (lynx -dump filename) and got the below results: Title ID Owner Priority Estimate Project Change Date Changed By Complexity Create Date Created By Detail Estimate Total De tail... (6 Replies)
Discussion started by: bi.infa
6 Replies

7. Shell Programming and Scripting

Retaining the Unix CSV format in Excel format while exporting

Hi All, I have created a Unix Shell script whch creates a *.csv file and export it to Excel. The problem i am facing is that Users wants one of the AMOUNT field in comma separted values. Example : if the Amount has the value as 3000000 User wants to be in 3,000,000 format. This Amount format... (2 Replies)
Discussion started by: rawat_me01
2 Replies

8. UNIX for Advanced & Expert Users

shell script to format .CSV data

Hi all, I have written a shell script to search a specified directory (e.g. /home/user) for a list of specific words (shown as ${TMPDIR}/wordlist below). The script works well enough, but I was wondering if there was a way to display the line number that the word is found on? Thanks! cat... (1 Reply)
Discussion started by: tmcmurtr
1 Replies

9. Shell Programming and Scripting

Shell script to format a .CSV data

Hi There I needed to write a Unix shell script which will pick up the data from a .CSV file and reformat it as per the requirement and write it to another .CSV file. Currently I am in the proess of Data Import to "Remedy System" (A one kind of incident mangement Application) and this... (8 Replies)
Discussion started by: Uday1982
8 Replies
Login or Register to Ask a Question