Jump to content

Replace white space with commas?


Stasis
 Share

Recommended Posts

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

  • Developers

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

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

  • Developers

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

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

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

  • Developers

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

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

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...