DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Frep - commandline find and replace  (Read 9,430 times)

justice

  • Solve issues simply.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,898
    • Participated in NANY Challenge Four Times
    • Cody Award
    • Coding Snacks
    • DJ Badge - DJ
Frep - commandline find and replace
« on: August 02, 2010, 09:22 AM »
Needed a small utility to use in batch files to find and replace text in a file.
Code: (autohotkey)
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
TheFile = %1%
TheSearch = %2%
TheReplace = %3%
if 0 < 3 
{
    MsgBox This script requires at least 3 incoming parameters but it only received %0%.
    ExitApp
}
FileRead, Contents, %TheFile%
StringReplace, Contents, Contents, %TheSearch%, %TheReplace%, A 
FileDelete, %TheFile%
FileAppend, %Contents%,  %TheFile%
Contents = 

An example:
Code
>type test.txt
greg is a lovely guy
>frep test.txt greg john
>type test.txt
john is a lovely guy

I'm using it for a tiny templating engine for CSS files ;)
« Last Edit: August 02, 2010, 09:25 AM by justice »

Tuxman

  • OMG not him again!
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,522
    • Participated in NANY Challenge Seven Times
Re: Frep - commandline find and replace
« Reply #1 on: August 03, 2010, 04:06 AM »
Now why don't you use sed instead?  8)
I bet when Cheetahs race and one of them cheats, the other one goes "Man, you're such a Cheetah!" and they laugh & eat a zebra or whatever.

justice

  • Solve issues simply.
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,898
    • Participated in NANY Challenge Four Times
    • Cody Award
    • Coding Snacks
    • DJ Badge - DJ
Re: Frep - commandline find and replace
« Reply #2 on: August 03, 2010, 10:21 AM »
Because I don't know the sed syntax and it was more fun to write a script that works how I expect it should work :) Could someone move this to the finished snacks section please? :)

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 3,217
Re: Frep - commandline find and replace
« Reply #3 on: August 04, 2010, 09:16 PM »
For once I agree with Tuxman  8)  Though I used AWK more because AWK scripts are easier to maintain.  Another option is Minitrue by Andrew Pipkin, which is specifically designed for the job.  It's worth reading Eric Pement's sed page for more details on this; keep paging down to the "related batch editing files" section which mentions Minitrue amongst others.  On no account overlook Mr. Pement's supplementary page on Minitrue workarounds.  Mr. Pement appears unaware that Jason Hood has made an updated version of MiniTrue that fixes some issues, though I haven't tried it.

Original address: https://www.donationcoder.com/forum/index.php?topic=23606.0