Solved

Text manipulation

Posted on 2004-08-26
7
271 Views
Last Modified: 2008-02-26
Ok, I've got a list of users in the following format:

20051450:firstName lastName
20061579:firstName middleName lastName
...

The number before the name is the student ID number which will be the student's username.  I need to convert that list into a new list so I can use ldifde to import the users into AD on a Win2k server.  This is what the first entry above would need to look like after conversion:

dn: CN="firstName lastName",OU=Students,DC=prendergast,DC=com
changetype: add
cn: firstName lastName
objectclass: user
displayName: firstName lastName
givenName: firstName
name: firstName lastName
SAMAccountName: 20051450
sn: lastName
userPrincipalName: 20051450@prendergast.com

I've got over 900 users to enter, doing this by hand just isn't an option.  I had a PHP script that could do it before but don't have it anymore.  Any help is appreciated.  Thanks!
0
Comment
Question by:salvagbf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:ozgulkaan
ID: 11905458
open "c:\NAMEOFFILE.txt" for input as 1
open "c:\RESULTFILE.txt" for output as 2

do while not eof(1)
  line input #1,rowdata
  plc = instr(rowdata,":")

 
  dn: CN="firstName lastName",OU=Students,DC=prendergast,DC=com
  changetype: add
  cn: firstName lastName
  objectclass: user
  displayName: firstName lastName
  givenName: firstName
  name: firstName lastName
  SAMAccountName: 20051450
  sn: lastName
  userPrincipalName: 20051450@prendergast.com
loop
0
 
LVL 6

Author Comment

by:salvagbf
ID: 11905592
Thanks.. umm... how do I implement this?
0
 
LVL 2

Accepted Solution

by:
b0bmaster earned 500 total points
ID: 11905668
Here is a VBScript i made copy/paste it into notepad and save it as "Process.VBS"

------------------------------------------------------------------------------------------------------------------

  Set FSO= CreateObject("scripting.filesystemobject")
  Set InFile = FSO.GetFile("students.txt")
  Set OutFile = FSO.OpenTextFile("output.txt", 2, Not FSO.FileExists("output.txt"))
  Set TextStream = InFile.OpenAsTextStream(1)
 
   Do While Not TextStream.AtEndOfStream
   
    Line = TextStream.ReadLine
    ArrayX = Split(Line,":")
    StudentID = Trim(ArrayX(0))
    temp = Split(ArrayX(1)," ")
    StudentFirstName = Trim(temp(LBound(temp)))
    StudentSurName = Trim(temp(UBound(temp)))

    OutFile.Write "dn: CN=" + Chr(34) + StudentFirstName + " " + StudentSurName + Chr(34) + ",OU=Students,DC=prendergast,DC=com" & vbNewLine
    OutFile.Write "changetype: add" & vbNewLine
    OutFile.Write "cn: " + StudentFirstName + " " + StudentSurName & vbNewLine
    OutFile.Write "objectclass: user" & vbNewLine
    OutFile.Write "displayName: " + StudentFirstName + " " + StudentSurName & vbNewLine
    OutFile.Write "givenName: " + StudentFirstName & vbNewLine
    OutFile.Write "name: " + StudentFirstName + " " + StudentSurName & vbNewLine
    OutFile.Write "SAMAccountName: " + StudentID & vbNewLine
    OutFile.Write "sn: " + StudentSurName & vbNewLine
    OutFile.Write "userPrincipalName: " + StudentID + "@prendergast.com" & vbNewLine
    OutFile.Write vbNewLine
    OutFile.Write vbNewLine

   Loop

  TextStream.Close

---------------------------------------------------------------------------------------------------------------

Name the input file "students.txt" with the data as described in your question.
The output will be "output.txt"  

Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:ozgulkaan
ID: 11905725
the below is tested with visual basic. but to use it as a vbscript it needs some changes.
put all the data in a text file, which is below NAMEOFFILE.txt
and run visual basic. it's done.

--------------------------------------------------------------------------------------------------

Open "c:\NAMEOFFILE.txt" For Input As 1
Open "c:\RESULTFILE.txt" For Output As 2

Do While Not EOF(1)
  Line Input #1, rowdata
  rowdata = Trim(rowdata)
  plc = InStr(rowdata, ":")
  StudentNo = Left(rowdata, plc - 1)
  rowdata = Right(rowdata, Len(rowdata) - plc)
  plc = InStr(rowdata, " ")
  StudentName = Left(rowdata, plc - 1)
  rowdata = Right(rowdata, Len(rowdata) - plc)
  plc = InStr(rowdata, " ")
  If plc = 0 Then 'if no middlename exists
    StudentLastName = rowdata
  Else
    StudentMiddleName = Left(rowdata, plc - 1)
    rowdata = Right(rowdata, Len(rowdata) - plc)
    StudentLastName = rowdata
  End If

  print#2, "dn: CN=""" & StudentName & " " & StudentLastName & """,OU=Students,DC=prendergast,DC=com"
  print#2, "changetype: add"
  print#2, "cn: " & StudentName & " " & StudentLastName
  print#2, "objectclass: user"
  print#2, "displayName: " & StudentName & " " & StudentLastName
  print#2, "givenName: " & StudentName
  print#2, "name: " & StudentName & " " & StudentLastName
  print#2, "SAMAccountName: " & StudentNo
  print#2, "sn: " & StudentLastName
  print#2, "userPrincipalName: " & StudentNo & "@prendergast.com"

Loop

close #1,2
0
 
LVL 6

Author Comment

by:salvagbf
ID: 11906467
Wow, awesome! Thank you both! I'm leaving for today but will test out your solutions and award points tomorrow, thank you both sooo much!
0
 
LVL 4

Expert Comment

by:LittleRed1
ID: 11908703
Easier with a batch file and no dependencies.

Like this:

::-------------------------------------------------------------------------------------
@echo off

setlocal ENABLEDELAYEDEXPANSION

for /f "tokens=1-4 delims=: " %%a in (userlist.txt) do (
      if "%%d" == "" (
            :: Create temporary file if no middle name is present
            echo dn: CN="%%b %%c",OU=Students,DC=prendergast,DC=com>tmp.txt
            echo changetype: add>>tmp.txt
            echo cn: %%b %%c>>tmp.txt
            echo objectclass: user>>tmp.txt
            echo displayName: %%b %%c>>tmp.txt
            echo givenName: %%b>>tmp.txt
            echo name: %%b %%c>>tmp.txt
            echo SAMAccountName: %%a  >>tmp.txt
            echo sn: %%c>>tmp.txt
            echo userPrincipalName: %%a@prendergast.com>>tmp.txt
      ) else (
            :: Create temporary file if middle name is present, include middle name in name: field
            echo dn: CN="%%b %%d",OU=Students,DC=prendergast,DC=com>tmp.txt
            echo changetype: add>>tmp.txt
            echo cn: %%b %%d>>tmp.txt
            echo objectclass: user>>tmp.txt
            echo displayName: %%b %%d>>tmp.txt
            echo givenName: %%b>>tmp.txt
            echo name: %%b %%c %%d>>tmp.txt
            echo SAMAccountName: %%a  >>tmp.txt
            echo sn: %%d>>tmp.txt
            echo userPrincipalName: %%a@prendergast.com>>tmp.txt
      )
      
      :: Call LDIFDE here, add whatever additional switches you need.
      ldifde -i -f tmp.txt
)
::-------------------------------------------------------------------------------------
0
 

Expert Comment

by:maxrizzato
ID: 11912962
You can use gawk (GNU Awk is a batch text editor), found here, foe example:

http://unxutils.sourceforge.net

(together with a host of other *nix utils natively ported to Windows).

With awk there are many ways you can accomplish your goal, following is an example of the code you can use.

script.awk:

BEGIN{     FS="[: ]";
      termination = 1;
      getline;
      do{

            #FS="[: ]" sets colon and whitespace as field separators
            #getline reads a single line from input textfile
            #$0 is the entire line; $1 is the first field and so on.
            #$(NF) is the last field of the line, referenced using the internal variable NF that is set
            #to the number of fields in the line each time getline is called
            #as an example, $(i) can reference each field in the line,
            #if you make i cycle from 1 to NF

            printf("dn: CN=\"%s %s\",OU=Students,DC=prendergast,DC=com\n", $2, $(NF));  
            printf("changetype: add\n");
            printf("cn: %s %s\n", $2, $(NF));
            printf("objectclass: user\n");
            printf("displayName: %s %s\n", $2, $(NF));
            printf("givenName: %s\n", $2);
            printf("name: %s %s\n", $2, $(NF));
            printf("name: %s %s\n", $2, $(NF));                        
            printf("SAMAccountName: %s\n", $1);
            printf("sn: %s\n", $(NF));
            printf("userPrincipalName: %s@prendergast.com\n", $1);
            printf("\n");        #Just to separate stanzas. Comment it out if you don't want
                                            # a separation line

      }while(getline> 0)
}

You can invoke the above script from a cmd windows shell this way:

C:\>type inputfile.txt | gawk -f script.awk > output.txt

in output.txt you get the results.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction How to create multiboot configuration with XP\Vista and Windows 7 on it? And most important question - how to do this correctly so not to have any kind of nightmares we get when system gets screwed? First of all one should realize t…
In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question