polingkyle Posted October 5, 2012 Share Posted October 5, 2012 First time posting here. I have a text document with a bunch of information in it. The document is formatted as: "Random Text: Last First MI, text text text, text text Last First MI, text text text, text text Last First MI, text text text, text text ....." I'm trying to reformat all of the names as First MI Last instead of Last First MI. What I did was replace all of the carriage breaks with a # sign and then told it to find everything between the # and , and save it as a variable. My problem is, since the first name has 2 extra carriage breaks, it is including "##" in the variable for the last name. I can't figure out how to get it to remove the extra 2 # signs and then repeat itself through the entire document until all names have been fixed. Any help would be greatly appreciated! Here's what I have so far: expandcollapse popup#include #Include #include Local $username = EnvGet("USERNAME") Local $userprofile = EnvGet("USERPROFILE") $TextFileName = $userprofile & "\Desktop\MuniCourt.txt" WinActivate ("MuniCourt.txt") Send("^s") WinClose ("MuniCourt.txt") ;fixes midline returns $FileContents = FileRead($TextFileName) $FileContents = StringReplace($FileContents, @CR,"#") FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) $FileContents = 0 $FileContents = FileRead($TextFileName) $FileContents = StringReplace($FileContents, @CRLF,"#") FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) $FileContents = 0 ;NameWizard (Changes "Last, First MI" to "First MI Last") ;First person's name is corrected $FileContents = FileRead($TextFileName) $Name = _StringBetween($FileContents, '#', ',') If @error Then Exit $FullName = _StringProper(StringRegExpReplace($Name[0], '###', ' ')) $Lastname = _StringBetween($FullName, '', ' ') $Firstname = _StringBetween($FullName, ' ', '') $FileContents = FileRead($TextFileName) $FileContents = StringReplace($FileContents, $Lastname[0] & " " & $Firstname[0], $Firstname[0] & " " & $Lastname[0]) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) $FileContents = 0 $FileContents = FileRead($TextFileName) $FileContents = StringReplace($FileContents, "#" & $Firstname[0], @CRLF & @CRLF & $Firstname[0]) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) $FileContents = 0 $FileContents = FileRead($TextFileName) $FileContents = StringReplace($FileContents, ":#", ":",1) FileDelete($TextFileName) FileWrite($TextFileName,$FileContents) $FileContents = 0 ;;TESTING MsgBox (1, "Name Variables", "First Name = " & $Firstname[0] & @CR & @CR & "Last Name = " & $Lastname[0] & @CR & @CR & "Full Name = " & $Fullname) ;;TESTING ;Reset variables $Name = 0 $FullName = 0 $Firstname = 0 $Lastname = 0 $FileContents = 0 Thank you in advance! Kyle P. Link to comment Share on other sites More sharing options...
iamtheky Posted October 5, 2012 Share Posted October 5, 2012 post a small example text file, or post the exact contents into tags. There is probably an easier way to parse and adjust the text, but it would reduce a lot of guess and check if you would post actual content. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
polingkyle Posted October 5, 2012 Author Share Posted October 5, 2012 (edited) Thank you for the quick response. Below is the exact document that I'm working with. This document changes every week but the formatting should always be the same. <snip> polingkyle, I have removed that text - I am not at all sure you should have posted it. if you want to repost it, please do it with anonymised names (e.g. A**** F****). Edited October 5, 2012 by Melba23 Removed data Link to comment Share on other sites More sharing options...
jdelaney Posted October 5, 2012 Share Posted October 5, 2012 I'm thinking you should have annonymized this information IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
bogQ Posted October 5, 2012 Share Posted October 5, 2012 (edited) how whud you reform this name? Bickar Edwin Iii J and this name? Fenner Kevin one have extended name other dont have middle name Edited October 5, 2012 by bogQ TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.  Link to comment Share on other sites More sharing options...
Spiff59 Posted October 5, 2012 Share Posted October 5, 2012 (edited) Technically, you're screwed. Whomever compiles that list needs to provide better delimiting. We have patients with "2-part" last names. We went through a lot of headache with recent rule changes that require electronic claims for those patients to be transmitted as exact matches of their legal names. So, if for example, we have a patient first name "Chief" and last name "Chicken Feathers", we have to transmit the last name with the space. If you encounter a 2-part last name, there is no way you can correctly parse it. (ps - if those are actual names, and I had posted them, the HIPAA police would be here by now). I also see that a name suffix can be inserted between the first name and middle initial... Edited October 5, 2012 by Spiff59 Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted October 5, 2012 Moderators Share Posted October 5, 2012 (edited) Looks like you are pulling from either mugshot-catalog.com or whosarrested.com, but I agree you still should have provided an example of the text you seek rather than putting names and addresses out there for all the world to see. Edited October 5, 2012 by JLogan3o13 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
polingkyle Posted October 5, 2012 Author Share Posted October 5, 2012 It's public records so anonymity is not necessary. I set the variables up so the first word of each parapraph is the last name and everything else until the first comma is the first name.Bickar Edwin Iii J would become Edwin Iii J Bickar.Fenner Kevin would still become Kevin Fenner.It doesn't need to be perfect because we have someone that proofreads everything before finalizing. Thanks again for everyone's help on this. Unfortunately, I can only have 2 more posts today since I'm a new user to the forum.... Link to comment Share on other sites More sharing options...
Developers Jos Posted October 5, 2012 Developers Share Posted October 5, 2012 I can only have 2 more posts today since I'm a new user to the forum....You should be fine now.. SciTE4AutoIt3 Full installer Download page  - Beta files    Read before posting   How to post scriptsource   Forum etiquette Forum Rules  Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
polingkyle Posted October 5, 2012 Author Share Posted October 5, 2012 You should be fine now.. Awesome! Thanks!Kyle Link to comment Share on other sites More sharing options...
kylomas Posted October 5, 2012 Share Posted October 5, 2012 (edited) polingkyle, In response to your origional question: stringreplace($mystring,'##','#') will eliminate dup "#"'s. kylomas edit: are you sure that you do not have any @lf's? Edited October 5, 2012 by kylomas Forum Rules        Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
bogQ Posted October 5, 2012 Share Posted October 5, 2012 i whud do it something like this #include <String.au3> #include <Array.au3> ;~ First MI Last ;~ Last First MI $return = _SomeFunc('Bickar Edwin Iii J, text1 text1 text1, text2 text2') MsgBox(0,'',$return) $return = _SomeFunc('Crowley Bradley K, text1 text1 text1, text2 text2') MsgBox(0,'',$return) $return = _SomeFunc('Kevin Fenner, text1 text1 text1, text2 text2') MsgBox(0,'',$return) Func _SomeFunc($MainData) Local $first = StringSplit($MainData,',') Local $second = _StringExplode($first[1],' ',1) $first[1] = $second[1]&' '&$second[0] Return _ArrayToString($first,',',1) EndFunc i whud read the file to array with _FileReadToArray the use For next loop to circle the array and replace data with returned valye of _SomeFunc($MainData) after that i whud write new file with _FileWriteFromArray TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.  Link to comment Share on other sites More sharing options...
jdelaney Posted October 5, 2012 Share Posted October 5, 2012 (edited) one more time something like this: #include <Array.au3> $string = "Ali Omar Sheariff, 31, ofAve, violation of tpo, continued." & @CRLF & _ "asdf asdf asdf, 31, of , violation of tpo, continued." & @CRLF & _ "asdfi adsfasdf Sasdfasdfff, 31, of , violation of tpo, continued, pd apptd, no contact." & @CRLF & _ "asdfsdf asdfasd test, 40, Dr, theft, bench warrant ordered." & @CRLF & _ "asd fasdfas dfMarie, 46, Rd, felonious assault/weapon, dismissed." & @CRLF & _ "sadfa sdasfdasdf Lee, 30, of Ave, theft, continued, theft, continued." & @CRLF & _ "asdfsad asd fsafd, 22, of Ave, theft, continued, pd apptd." & @CRLF & _ "asdfsdf sadfsdf E, 52, of Ct, ovi, continued, open container/vehicle, continued, display of license plates, continued." $atest1 = StringRegExp ( $string, "([ws]+),.*", 3 ) For $i = 0 To UBound ( $atest1 ) - 1 $atest2 = StringRegExp ( $atest1[$i], "([w]+)s?", 3 ) $fn = "" $ln = "" $mi = "" For $j = 0 To UBound ( $atest2 ) - 1 Switch $j Case 0 $ln = $atest2[$j] Case 1 $fn = $atest2[$j] Case Else $mi_setup = $atest2[$j] & " " $mi &= $mi_setup EndSwitch Next If stringlen ( $mi ) > 0 Then $mi = StringSTripWS ( $mi, 7 ) $string_replace = $fn & " " & $ln & " " & $mi Else $string_replace = $fn & " " & $ln EndIf $string = StringReplace ( $string, $atest1[$i], $string_replace ) Next MsgBox ( 1,1,$string) Edited October 5, 2012 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
polingkyle Posted October 5, 2012 Author Share Posted October 5, 2012 polingkyle, In response to your origional question: stringreplace($mystring,'##','#') will eliminate dup "#"'s. kylomas edit: are you sure that you do not have any @lf's? When I try that like that, the first line still shows as "Ian Calen # # Arnold, 20..." When I view the text in Notepad, it doesn't show the carriage breaks. When I paste it here, they show up. That's probably part of the problem, right? Do I need to do: $FileContents = StringReplace($FileContents, @CR & "#" & @CR & "#" & @CR,"#") Link to comment Share on other sites More sharing options...
Spiff59 Posted October 5, 2012 Share Posted October 5, 2012 (edited) And another... #include <Array.au3> #include <File.au3> ; simulate a _FileReadToArray() of your file Global $aFile[9] = [ 8, _ "Blow Joe", _ "Public John Q", _ "Leary Timothy Sr", _ "Leary Timothy Jr", _ "Leary Timothy II", _ "Leary Timothy III", _ "Leary Timothy IV", _ "Wayne John Sr M"] _ArrayDisplay($aFile) For $x = 1 to $aFile[0] $sNewName = "" $sSuffix = "" $aTemp = StringSplit($aFile[$x], " ") For $y = 2 to $aTemp[0] ; bypass last name for now Switch $aTemp[$y] Case "Sr", "Jr", "II", "III", "IV" $sSuffix = $aTemp[$y] ; kick any suffix out until later Case Else $sNewName &= $aTemp[$y] & " " ; build new name string EndSwitch Next $sNewName &= $aTemp[1] ; append last name If $sSuffix Then $sNewName &= " " & $sSuffix ; append suffix if present $aFile[$x] = $sNewName Next _ArrayDisplay($aFile) Edit: added comments Edited October 5, 2012 by Spiff59 Link to comment Share on other sites More sharing options...
Developers Jos Posted October 5, 2012 Developers Share Posted October 5, 2012 what about an oldschool approach: Local $ifile = FileOpen("test.txt", 0) Local $ofile = FileOpen("testout.txt", 2) ; Check if file opened for reading OK If $ifile = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in lines of text until the EOF is reached While 1 Local $i_Rec = FileReadLine($ifile) If @error = -1 Then ExitLoop $i_Name = StringLeft($i_Rec, StringInStr($i_Rec,",")-1) $o_Rec = StringMid($i_Rec, StringInStr($i_Rec,",")+1) $o_lName = StringLeft($i_Name, StringInStr($i_Name," ")-1) $o_fName = StringMid($i_Name, StringInStr($i_Name," ")+1) FileWriteLine($ofile,$o_fName & " " & $o_lName & "," & $o_Rec) WEnd FileClose($ifile) FileClose($ofile) Jos SciTE4AutoIt3 Full installer Download page  - Beta files    Read before posting   How to post scriptsource   Forum etiquette Forum Rules  Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
kylomas Posted October 5, 2012 Share Posted October 5, 2012 pokingkyle, Without access to the EXACT file that you are processing we are only guessing! kylomas Forum Rules        Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
polingkyle Posted October 5, 2012 Author Share Posted October 5, 2012 one more time something like this: #include <Array.au3> $string = "Ali Omar Sheariff, 31, of 35 E Madison Ave, violation of tpo, continued." & @CRLF & _ "Ali Omar Sheariff, 31, of 35 E Madison, violation of tpo, continued." & @CRLF & _ "Ali Omar Sheariff, 31, of 35 E Madison Ave, violation of tpo, continued, pd apptd, no contact." & @CRLF & _ "Beaty Lisa Annette test, 40, of 1936 Jordan Dr, theft, bench warrant ordered." & @CRLF & _ "Brandon Katherine Marie, 46, of 822 Selma Rd, felonious assault/weapon, dismissed." & @CRLF & _ "Everhart Timothy Lee, 30, of 416 Willard Ave, theft, continued, theft, continued." & @CRLF & _ "Jackson Desirah Lashay, 22, of 1361 Woodward Ave, theft, continued, pd apptd." & @CRLF & _ "Mcmillen Barry E, 52, of 3801 Quail Hollow Ct, ovi, continued, open container/vehicle, continued, display of license plates, continued." $atest1 = StringRegExp ( $string, "([ws]+),.*", 3 ) For $i = 0 To UBound ( $atest1 ) - 1 $atest2 = StringRegExp ( $atest1[$i], "([w]+)s?", 3 ) $fn = "" $ln = "" $mi = "" For $j = 0 To UBound ( $atest2 ) - 1 Switch $j Case 0 $ln = $atest2[$j] Case 1 $fn = $atest2[$j] Case Else $mi_setup = $atest2[$j] & " " $mi &= $mi_setup EndSwitch Next If stringlen ( $mi ) > 0 Then $mi = StringSTripWS ( $mi, 7 ) $string_replace = $fn & " " & $ln & " " & $mi Else $string_replace = $fn & " " & $ln EndIf $string = StringReplace ( $string, $atest1[$i], $string_replace ) Next MsgBox ( 1,1,$string) Since the document changes content everyday, how would I get the data input into this script? I'm pretty new to arrays... Kyle Link to comment Share on other sites More sharing options...
bogQ Posted October 5, 2012 Share Posted October 5, 2012 @jdelaney i think that from string 1 2 3 you did2 1 3instead3 1 2or i did not understand needed end result TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.  Link to comment Share on other sites More sharing options...
polingkyle Posted October 5, 2012 Author Share Posted October 5, 2012 (edited) edit: are you sure that you do not have any @lf's?No I'm not sure. How would I know? Edited October 5, 2012 by polingkyle Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now