ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Finished Programs

Frep - commandline find and replace

(1/1)

justice:
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:

--- --->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 ;)

Tuxman:
Now why don't you use sed instead?  8)

justice:
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:
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.

Navigation

[0] Message Index

Go to full version