Stasis Posted September 19, 2007 Share Posted September 19, 2007 I am trying to find a way to replace all of the white spaces (tabs, multiple tables, spaces, multiple spaces) with commas in order to convert a text report of data into a csv file which could be opened in excel. I made a script which opens the text file and saves only the lines I want however I cant get anything to work to swap the white space. I was trying the StringStripWS however I couldnt get it to replace them with commas. Link to comment Share on other sites More sharing options...
Developers Jos Posted September 19, 2007 Developers Share Posted September 19, 2007 StringReplace() should do the job ... 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...
Stasis Posted September 19, 2007 Author Share Posted September 19, 2007 StringReplace() should do the job ...But I cant get it to replace multiple spaces in a row, multiple tabs or a combination of tabs and spaces with just a single comma between the columns of data. Link to comment Share on other sites More sharing options...
Zedna Posted September 19, 2007 Share Posted September 19, 2007 Look at StringStripWS, maybe this help you. Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Developers Jos Posted September 19, 2007 Developers Share Posted September 19, 2007 try this: $test="this is " & @TAB & @TAB & " a test" $test = StringRegExpReplace($test,"(\s+)",",") ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @crlf) 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...
Stasis Posted September 19, 2007 Author Share Posted September 19, 2007 try this: $test="this is " & @TAB & @TAB & " a test" $test = StringRegExpReplace($test,"(\s+)",",") ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @crlf) Ok, im new to autoit, not quite sure how I would put that into what I have. Here is my script so far: #notrayicon $input = fileopen("c:\ccnt.txt",0) $output = fileopen("c:\ccnt.tmp",2) ;Check if the files were opened successfully. If $input = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf If $output = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;Parse the text beginning with a HUID while 1 $inline = filereadline($input) if $inline = -1 then exitloop if stringinstr($inline,"H8") then filewriteline($output,$inline) endif wend fileclose($input) fileclose($output) Link to comment Share on other sites More sharing options...
Developers Jos Posted September 19, 2007 Developers Share Posted September 19, 2007 Something like this ? #notrayicon $input = FileOpen("c:\ccnt.txt", 0) $output = FileOpen("c:\ccnt.tmp", 2) ;Check if the files were opened successfully. If $input = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf If $output = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;Parse the text beginning with a HUID While 1 $inline = FileReadLine($input) If @error Then ExitLoop If StringInStr($inline, "H8") Then FileWriteLine($output, StringRegExpReplace($inline, "(\s+)", ",")) EndIf WEnd FileClose($input) FileClose($output) 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...
Stasis Posted September 19, 2007 Author Share Posted September 19, 2007 Something like this ? #notrayicon $input = FileOpen("c:\ccnt.txt", 0) $output = FileOpen("c:\ccnt.tmp", 2) ;Check if the files were opened successfully. If $input = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf If $output = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;Parse the text beginning with a HUID While 1 $inline = FileReadLine($input) If @error Then ExitLoop If StringInStr($inline, "H8") Then FileWriteLine($output, StringRegExpReplace($inline, "(\s+)", ",")) EndIf WEnd FileClose($input) FileClose($output) Jos Works great! Thanks!! Link to comment Share on other sites More sharing options...
Stasis Posted September 20, 2007 Author Share Posted September 20, 2007 Is there any way I could save the lines in the new file based on the H plus 10 unknown numbers (such as a wild card) instead of looking for H8 only? Link to comment Share on other sites More sharing options...
flyingboz Posted September 20, 2007 Share Posted September 20, 2007 Is there any way I could save the lines in the new file based on the H plus 10 unknown numbers (such as a wild card) instead of looking for H8 only?yes. Reading the help file before you post... Not only will it make you look smarter, it will make you smarter. Link to comment Share on other sites More sharing options...
Stasis Posted September 20, 2007 Author Share Posted September 20, 2007 yes.How? I tried * but that doesnt work. Thanks! Link to comment Share on other sites More sharing options...
Developers Jos Posted September 20, 2007 Developers Share Posted September 20, 2007 Is there any way I could save the lines in the new file based on the H plus 10 unknown numbers (such as a wild card) instead of looking for H8 only?Can you be more specific ? 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...
aslani Posted September 20, 2007 Share Posted September 20, 2007 Just for reference, check this thread out;http://www.autoitscript.com/forum/index.ph...=53513&st=0You might pick-up something useful on what I did. [font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version Link to comment Share on other sites More sharing options...
Stasis Posted September 20, 2007 Author Share Posted September 20, 2007 Can you be more specific ?Currently it looks for H8 in the line, if it does it copies that line to the tmp file. Id rather it look for the H and then 9 unknown numbers following, such as H*********. I think all of the codes will start with H8 however it would be better to search for H followed by 9 wildcards. Link to comment Share on other sites More sharing options...
flyingboz Posted September 20, 2007 Share Posted September 20, 2007 (edited) Id rather it look for the H and then 9 unknown numbers following, such as H*********. I think all of the codes will start with H8 however it would be better to search for H followed by 9 wildcards.You're going to have to learn to think a little bit....One method. If StringLeft($text,2) == "H8" OR StringLeft($text,2) == "H9" Then.... For true pattern matching using regular expressions, refer to the manual. [edit:] For Extra credit: 1) What is the difference between what matches using StringInStr() vs. StringLeft()? 2) What does the "==" operator signify? How is it different from "=" ? Edited September 20, 2007 by flyingboz Reading the help file before you post... Not only will it make you look smarter, it will make you smarter. 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