[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

transform ldif to CSV with selected values

Hello all,
I need to transform an ldif file consisting of multiple records like this..

cn: Jon Sasquatch
lockoutTime: 0
url: GPS Inquiry/HORIZ
url: GIS Inquiry/HORIZ
url: CSR/HORIZ
url: GPS Inquiry/WLPT

Where I need to capture the cn and each url line and I would like to have this in csv format

Jon Sasquatch,GPS Inquiry/HORIZ,GIS Inquiry/HORIZ
Jon Sasquatch,GPS Inquiry/HORIZ,GIS Inquiry/HORIZ


I can get the indiviual lines with a begin or ^ function but its the next begin and grouping that I cannot figure out as well as the CSV formatting.

I'd prefer to use VBA, awk, sed, or shell if possible, but can look at other options.
Thank you
0
ruggerrick
Asked:
ruggerrick
  • 2
1 Solution
 
aikimarkCommented:
why would you want to repeat the first two entries and ignore the second two entries?
0
 
aikimarkCommented:
Once you've read the LDIF file (text is expected to be in a variable. strLDIFtext used as variable name in the following example), you can parse and process the text as follows.  I left a comment where you would put code to output the line of data.
dim oRE as object
dim oMatches as object, oMatch as object
dim strOut
set oRE = createobject("vbscript.regexp")
oRE.Global = True
oRE.Pattern = "(cn|url): (.*)"
if oRE.Test(strLDIFtext) Then
  set oMatches = oRE.Execute(strLDIFtext)
  for each oMatch in oMatches
    if oMatch.Submatches(0) = "url" then
      if len(strOut) <> 0 Then
        'output strOut to file
      end if
      strOut = oMatch.Submatches(1)
    else
      strOut = strOut & "," & oMatch.Submatches(1)
    endif
  next
else
  msgbox "LDIF data does not match expected pattern"
end if

Open in new window

Note: all the url values for each cn value appear on the same line.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now