Sign in to follow this  
Followers 0
Stasis

Replace white space with commas?

15 posts in this topic

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.

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

try this:

$test="this  is " & @TAB & @TAB & " a      test"
$test = StringRegExpReplace($test,"(\s+)",",")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $test = ' & $test & @crlf)

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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!!

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

yes.

How? I tried * but that doesnt work. Thanks!

Share this post


Link to post
Share on other sites

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 ?

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#15 ·  Posted (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 by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0