Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

cflee

By: a guest on Nov 9th, 2007  |  syntax: Perl  |  size: 2.74 KB  |  hits: 286  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. # -
  2. # - csv2ldif.pl
  3. # -
  4. # -     This program takes CSV data and maps it to an update LDIF file
  5. # -
  6. # -     Pass the name of the input CSV file as the sole argument. The CSV should be
  7. # - in a format where the first row has column headings and the first column
  8. # - should be the DN (with header 'dn'). The output file will be the name of the
  9. # - input file with the .ldif extension appended.
  10. # -
  11. # - Usage:  perl csv2ldif.pl input.csv
  12. # -
  13. # -
  14. # -
  15. # - Heavily modified from Novell Cool Solutions: CSV to LDIF Converter
  16. # -     http://www.novell.com/coolsolutions/tools/14462.html
  17. # -
  18. # - Chiang Fong Lee <matik@cflee.net>, Nov 9 2007
  19. # -   This script now uses the Text::CSV module to handle CSVs properly -
  20. # -   original script did own parsing based on quotes and commas and broke when
  21. # -   confronted with output from CSVDE.
  22. # -  
  23. # -   Only remaining similarities with original FixData2.pl is variable names.
  24. # -
  25.  
  26. require FileHandle;
  27. require Text::CSV;
  28.  
  29. my($csvFile, $ldifType, $inputfile, $outfile);
  30. my($linecount, @fieldmap, $column);
  31.  
  32. # Get a Text::CSV object
  33. my $csv = Text::CSV->new;
  34.  
  35. # Check for lack of arguments
  36. if (@ARGV == 0) {
  37.         print "Usage: perl csv2ldif.pl input.csv\n\n";
  38.         print "See the source for more usage info.\n\n";
  39.         exit;
  40. }
  41.  
  42. $csvFile = $ARGV[0];
  43. $ldifType = "replace";
  44.  
  45. # Try to open source file and destination file
  46. $inputfile = new FileHandle($csvFile) ||
  47.         die "Can't open the import file!\n";
  48. $outfile = new FileHandle(">$csvFile.ldif") ||
  49.         die "Can't create or open the output file!\n";
  50.  
  51. ### REAL WORK BEGINS ###
  52.  
  53. # Output LDIF version header.
  54. print $outfile "version: 1\n\n";
  55.  
  56. $linecount = 0;
  57.  
  58. while(defined($line = <$inputfile>)) {
  59.         $linecount++;
  60.        
  61.         # Parse this line and get it into an array.
  62.         $csv->parse($line);
  63.         @columns = $csv->fields();
  64.        
  65.         # Read the first line of the data file and build the field names from it.
  66.         if($linecount == 1) {
  67.                 $fieldcount = 0;
  68.    
  69.     # Put every header field into the fieldmap array.
  70.                 foreach $cc (@columns) {
  71.                         @fieldmap[$fieldcount] = $cc;
  72.                         $fieldcount++;
  73.                 }
  74.                 next;
  75.         }
  76.        
  77.         # Now, for all other lines, output it in LDIF format, appropriately tagged
  78.         # with the headers from fieldmap
  79.         for($k = 0; $k < $fieldcount; $k++) {
  80.                 if($k == 0) {
  81.                         # Output DN first
  82.                         print $outfile "@fieldmap[$k]: ";
  83.                         print $outfile "@columns[0]\n";
  84.                         print $outfile "changetype: modify\n";
  85.                 } else {
  86.                         if($k > 1) {
  87.                                 # Output separator between attributes
  88.                                 print $outfile "-\n";
  89.                         }
  90.                         # Output change declaration, then new value
  91.                         print $outfile "$ldifType: @fieldmap[$k]\n";
  92.                         print $outfile "@fieldmap[$k]: @columns[$k]\n";
  93.                 }
  94.         }
  95.        
  96.         # Cleanup and close last change pair
  97.         print $outfile "-\n\n";
  98. }
  99.  
  100.  
  101. close $inputfile;
  102. close $outputfile;