Jump to content


Photo

Convert from .txt to other .txt format


  • Please log in to reply
13 replies to this topic

#1 para000

para000
  • Members
  • 185 posts

Posted 03 March 2012 - 01:32 PM

Hi.
I wanna ask you guys if anyone know how to make a scrip that do the fallowing:
I have a .txt file whit a lot of this line that look like this:


<Waypoint>[color=red]-9373.636 -961.6185 173.3781[/color]</Waypoint>

and i wanna convert every line (have like 500+ line) into:

<Hotspot X="[color=red]-9373.636[/color]" Y="[color=red]-961.6185[/color]" Z="[color=red]173.3781[/color]" />

do anyone know how to do it?
Thanks very much and if you need more details just ask.

#2 Guests

  • Guests

Posted 03 March 2012 - 01:36 PM

There will be dozens of ways to do it.
The most important thing you have to do is to learn regular expressions.

#3 Guests

  • Guests

Posted 03 March 2012 - 01:38 PM

<!-- m -->http://l.autohotkey....Ex-QuickRef.htm<!-- m -->

It is a million dollar worth for you right now.

Good Luck.

#4 para000

para000
  • Members
  • 185 posts

Posted 03 March 2012 - 01:39 PM

There will be dozens of ways to do it.
The most important thing you have to do is to learn regular expressions.


sorry buy i`m a beginner in programming . don't really know how to start it.

#5 Guests

  • Guests

Posted 03 March 2012 - 01:54 PM

One elementary answer of your question will go like this:

Var := "<Waypoint>-9373.636 -961.6185 173.3781</Waypoint>"
RegExMatch( Var, "U)<.+>([^ ]+) ([^ ]+) ([^ ]+)<.+>", O_Var )
Msgbox, <Hostpot X =" %O_var1% " Y=" %O_var2% " Z=" %O_var3% "/>

But, in a minute your needs change in a hundread ways. Nobody can make you happy. So, I said to you. STUDY.

#6 Guests

  • Guests

Posted 03 March 2012 - 02:00 PM

Study

Reads a file's contents into a variable.
Source: http://www.autohotke...ds/FileRead.htm

then parse the variable in a

Retrieves substrings (fields) from a string, one at a time.
Source: http://www.autohotke...s/LoopParse.htm

and save it to a new file with

Writes text to the end of a file (first creating the file, if necessary).
Source: http://www.autohotke.../FileAppend.htm

while parsing the file line by line you can use the following RE to rewrite the line and save it to a new file
line=<Waypoint>-9373.636 -961.6185 173.3781</Waypoint>
MsgBox % RegExReplace(line,"<Waypoint>(.*) (.*) (.*)</Waypoint>","<Hotspot X=""$1"" Y=""$2"" Z=""$3"" />")
Try something, post code if you get stuck.

Note that you can do the above in any decent text editor that supports a regular expression find/replace (such as notepad++)

#7 para000

para000
  • Members
  • 185 posts

Posted 03 March 2012 - 02:02 PM

k...i will try to understand it... thanks for your hlep

#8 para000

para000
  • Members
  • 185 posts

Posted 03 March 2012 - 02:05 PM

Study

Reads a file's contents into a variable.
Source: http://www.autohotke...ds/FileRead.htm

then parse the variable in a

Retrieves substrings (fields) from a string, one at a time.
Source: http://www.autohotke...s/LoopParse.htm

and save it to a new file with

Writes text to the end of a file (first creating the file, if necessary).
Source: http://www.autohotke.../FileAppend.htm

while parsing the file line by line you can use the following RE to rewrite the line and save it to a new file
line=<Waypoint>-9373.636 -961.6185 173.3781</Waypoint>
MsgBox % RegExReplace(line,"<Waypoint>(.*) (.*) (.*)</Waypoint>","<Hotspot X=""$1"" Y=""$2"" Z=""$3"" />")
Try something, post code if you get stuck.

Note that you can do the above in any decent text editor that supports a regular expression find/replace (such as notepad++)


in a editor is not working because i can`t replace something that not exist.

#9 Odlanir

Odlanir
  • Members
  • 754 posts

Posted 03 March 2012 - 02:52 PM

file=

(join`r`n

<Waypoint>-9373.143 -999.1444 666.2222</Waypoint>

<Waypoint>-1334.326 -741.7812 333.3333</Waypoint>

<Waypoint>-6667.455 -278.9638 999.4444</Waypoint>

<Waypoint>-2977.876 -658.1963 888.5555</Waypoint>

<Waypoint>-3496.681 -189.2561 444.6666</Waypoint>

)

run, notepad,,,PID

WinWaitActive, ahk_pid %PID% 

ControlSetText, Edit1,%file%,ahk_pid %PID% 

file := RegExReplace(File,"m`a)<Waypoint>(.*) (.*) (.*)</Waypoint>","<Hotspot X=""$1"" Y=""$2"" Z=""$3"" />")

MsgBox Now your data will change

ControlSetText, Edit1,%file%,ahk_pid %PID% 

return



esc::

	WinClose,ahk_pid %PID% 

	ExitApp

return


#10 para000

para000
  • Members
  • 185 posts

Posted 17 September 2012 - 10:11 AM

is there any way i can make the scrip read a .txt instead copying the text in the scrip. Because the script can`t read to may rows i have like 100 rows that i need to replace and i need to copy paste on the script every time 20 of them in script.

So i want to read a .txt the data replace it and post it on a new .txt

Thx very much if anyone can help me

Loop read, c:\data.txt
   newdata := A_LoopReadLine "`n" newdata

FileAppend %newdata%, c:\newdata.txt

This is a script that i have found on the forum that read data from a txt and place it on another .txt. Prob can help you.

Thx again.

#11 engunneer

engunneer
  • Fellows
  • 9162 posts

Posted 17 September 2012 - 12:29 PM

Study

Reads a file's contents into a variable.
Source: http://www.autohotke...ds/FileRead.htm



#12 para000

para000
  • Members
  • 185 posts

Posted 17 September 2012 - 01:42 PM

the point is that i don`t understand that much. I know some basic AHK but what you give me to read is very complicated. So if someone can show me i will be very grateful. i really don`t know how to make this.

sorry for asking this much.

#13 engunneer

engunneer
  • Fellows
  • 9162 posts

Posted 17 September 2012 - 07:07 PM

replace
file=
(join`r`n
<Waypoint>-9373.143 -999.1444 666.2222</Waypoint>
<Waypoint>-1334.326 -741.7812 333.3333</Waypoint>
<Waypoint>-6667.455 -278.9638 999.4444</Waypoint>
<Waypoint>-2977.876 -658.1963 888.5555</Waypoint>
<Waypoint>-3496.681 -189.2561 444.6666</Waypoint>
)
with
FileRead, file, C:\data.txt

file is the name of the variable that now holds the text from your file.

Alternately, your example from before
Loop read, c:\data.txt
   newdata := A_LoopReadLine "`n" newdata
;...
is basically the same thing, but inefficient. It does read the file into newdata.

I am hoping you understand the basic concept of variables?

#14 para000

para000
  • Members
  • 185 posts

Posted 17 September 2012 - 08:19 PM

Thx guys. I manage to make it whit your help.

here is the scrip if anyone on the forum will need it.

FileRead, file, C:\data.txt
file := RegExReplace(File,"m`a)  <Waypoint>(.*) (.*) (.*)</Waypoint>","<Hotspot X=""$1"" Y=""$2"" Z=""$3"" />")
FileAppend %file%, c:\newdata.txt
return

esc::
ExitApp
return

Thanks again guys.