Solved

Text manipulation

Posted on 2004-08-26
7
241 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now