Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Replace in text file


  • Please log in to reply
12 replies to this topic
skonvols2k
  • Members
  • 5 posts
  • Last active: Apr 30 2011 08:09 PM
  • Joined: 27 Feb 2008
Hi,
i need to search and replace multiple occurrence of same string in some text file , can someone point in right direction please?
Tnx

SmallGuy
  • Guests
  • Last active:
  • Joined: --
Try to use FileRead and StringReplace

for example:
FileRead, TheText, TextFile.txt
StringReplace, NewText, TheText, Chopin, Mozart, All

In example above, AutoHotkey will read Textfile.txt and save it to TheText variable.
In next step it will use StringReplace to change any "Chopin" it find to "Mozart". It will save into a NewText variable.

skonvols2k
  • Members
  • 5 posts
  • Last active: Apr 30 2011 08:09 PM
  • Joined: 27 Feb 2008

Try to use FileRead and StringReplace

for example:
FileRead, TheText, TextFile.txt
StringReplace, NewText, TheText, Chopin, Mozart, All

In example above, AutoHotkey will read Textfile.txt and save it to TheText variable.
In next step it will use StringReplace to change any "Chopin" it find to "Mozart". It will save into a NewText variable.


tnx a lot for quick response :-)

i wrote this:
FileRead, TheText, test.csv
StringReplace, NewText, TheText, ',', ';', All 
msgbox, %NewText%
FileDelete, test.csv
FileAppend, %NewText%, test.csv

but it doesn't work, maybe i'm doing something wrong with comma and semicolon ?
tnx

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
1) check out the Text file library <!-- m -->https://ahknet.autoh...ugov/tf-lib.htm<!-- m -->
2) check out the CSV library <!-- m -->http://www.autohotke...topic55909.html<!-- m -->
3) you need to escape the , with a backtic ` (not ' but a `) in your example

skonvols2k
  • Members
  • 5 posts
  • Last active: Apr 30 2011 08:09 PM
  • Joined: 27 Feb 2008
tnx now works

viakahk
  • Members
  • 8 posts
  • Last active: Feb 11 2015 05:54 PM
  • Joined: 21 Oct 2014

Hi is there a way to tweak this script to fit htm files? For instance:

FileRead, TheText, (Any File Name).htm

StringReplace, NewText, TheText, (character_X), (with character_A), All

The idea is I need to replace a set a characters from each htm file. I have a list of characters to search and replace so would I need a script for each character? Does anyone know if there is a way to search for list of characters within one file and replace them?

Please let me know. Thanks!
 


Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

You should be able to use skonvols2k's script and change his .csv file in for your .htm file

 

http://www.autohotke...le/#entry419584

 

And just do a bunch of StringReplace commands in a row, acting on the same variable like this:

 

StringReplace, NewText, TheText, (character_X), (with character_A), All
StringReplace, NewText, NewText, (character_Y), (with character_B), All
StringReplace, NewText, NewText, (character_Z), (with character_C), All


viakahk
  • Members
  • 8 posts
  • Last active: Feb 11 2015 05:54 PM
  • Joined: 21 Oct 2014

Thank you! Here is what I am trying:

FileRead, TheText, AnyHtmFile.htm (I want this to be able to accept any htm file without me having to enter the title)
StringReplace, NewText, TheText, `§`, `&#167;`, All 
StringReplace, NewText, NewText, `ˆ`, `&#94;`, All
StringReplace, NewText, NewText, `ü`, `&#252;`, All
FileDelete, AnyHtmFile.htm (I want this to be able to save/replace the input htm file without me having to do anything)
FileAppend, %NewText%, AnyHtmFile.htm (end with the input file being updated after all characters are searched and replaced)

I have to do this process over multiple times to different htm files and I have a list 33 characters to search/replace. I am trying this script but nothing seems to work. Got any ideas?



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
Probably because `§` isn't part of the text you are use `` - are you sure they are there that it isn't just § without the `` around them? You can use a simple array loop to condense your code see post below by Exaskryz

Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

You've got backticks (`) before commas. If you put them before a comma, that comma counts as escaped, which means it is not dividing parameters in the command but is instead being treated as a literal purposeful comma.

 

Right now your StringReplace is trying to replace the text "§, &#167;, All" with.. nothing

 

That's why it doesn't seem to be doing anything, because it doesn't find that string in your variable and won't erase it. Look at this:

string:="Something wicked, this way comes, today §, !"
StringReplace, test1, string, `§`, !, All   ; This will replace "§, !" with "All"
MsgBox % test1
StringReplace, test2, string, `§`, !,       ; This will delete "§, !"
MSgBox % test2 
StringReplace, test3, string, `§`,, ?, All ; This will replace "§," with "?"
MsgBox % test3
StringReplace, test4, string, `,, _, All ; This will replace all commas with underscores
MsgBox % test4
return

What you instead intended for was `§, `&#167;, All



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
Good catch Exaskryz

viakahk
  • Members
  • 8 posts
  • Last active: Feb 11 2015 05:54 PM
  • Joined: 21 Oct 2014

Thank you! This seems to work:

​StringReplace, test3, string, `§`,, &#167;, All ; This will replace "§," with "&#167;"
MsgBox % test3

However, I can't get it to read/replace the § within the htm file. All it does is create a blank htm file after the script is executed. I really appreciate all the help! 



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

I was noticing that § in my MsgBox was not rendering right (even though I'm on Unicode AHK). Try using the &#???; ID to represent that § maybe? Or Call it {U+00A7}.

 

Could you share the entirety of your script as you have it now so we can see what is going wrong? You shouldn't get a blank htm file unless string itself was blank.