Solved

Text manipulation

Posted on 2004-08-26
7
249 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Office applications not loading on Windows 8i 10 68
PXE question 7 84
Changing the installation path of this MSI 5 101
inplace upgrade from Windows 2003 R2 to 2012 8 72
Hello I read in a discussion about a person who configured a very simple mirror RAID with two hard drives; the system and data were on the same partition. He asked how to repair the system as it was not booting up anymore. In his case running …
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
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.

785 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