Have you ever found your self in a situation where you’ve needed to replace text in multiple files quickly and efficiently?
I came across this conundrum on a monthly basis when I needed to change the contents of tens/hundreds of LoadRunner scripts and parameters to point from the name of the preceding month to the name of the current month.
I did a bit of searching online and found this handy little command line utility called FART – Find and Replace Text. Put simply, it searches a file for X string and replaces it with Y.
It is really easy to use and literally takes a few minutes to set-up:
Usage: FART [options] [--] [,...] [find_string] [replace_string]
Options:
-h --help Show this help message (ignores other options)
-q --quiet Suppress output to stdio / stderr
-V --verbose Show more information
-r --recursive Process sub-folders recursively
-c --count Only show filenames, match counts and totals
-i --ignore-case Case insensitive text comparison
-v --invert Print lines NOT containing the find string
-n --line-number Print line number before each line (1-based)
-w --word Match whole word (uses C syntax, like grep)
-f --filename Find (and replace) filename instead of contents
-B --binary Also search (and replace) in binary files (CAUTION)
-C --c-style Allow C-style extended characters (\xFF\0\t\n\r\\ etc.)
--cvs Skip cvs dirs; execute "cvs edit" before changing files
-a --adapt Adapt the case of replace_string to found string
-b --backup Make a backup of each changed file
-p --preview Do not change the files but print the changes
Examples:
fart "C:\APRIL2011\Scripts\someFile.txt" oldText newText
This line instructs FART to simply replace the string oldText with newText.
fart -i -r "C:\APRIL2011\Scripts\*.prm" march2011 APRIL2011
This line will instruct FART to recursively (-r) search for any file with the .prm extension in the \Scripts folder. The -i flag tells FART to ignore case-sensitivity when looking for the string.
Download:
You can download the latest version of FART (1.99b) from the author’s homepage or from here
32 replies on “FARTing the Easy Way – Find And Replace Text”
great tool
any idea how I can go about replacing a text string that containts a colon?
The following doesnt work…
c:\windows\fart.exe –c-style “S:\misc_videos\Skate\*.wvx” G:\_Media_Center\ <Ref href=G:\_Media_Center\
Thanks
Hey Mark,
Try this and let me know if it works:
c:\windows\fart.exe "S:\misc_videos\Skate\*.wvx" G:\_Media_Center\ "<Ref href=G:\_Media_Center\
Notice the quotation mark before <ref – also notice that there isn’t one at the end – this is because the back slash would escape it.
Hope that helps π
cool stuff – thanks
How can we replace a sentences contain “\” with another one? For example, I want replace “C:\Program Files\ee\bin\test.bmp” with “D:\Documents and Setting\test2.bmp”, how should I do that? How to handle the two long strings? Hope you reply, thanks in advance!
Have you tried the following:
fart test.txt "C:\Program Files\ee\bin\test.bmp" "D:\Documents and Setting\test2.bmp"
And also use “fart” to edit text files in another work space. For example, I have “fart.ext” at C:\Program Files\FART, how can I use it to edit file at D:\test\123.txt at windows xp command line environement?
thank you!
For this you simply enter the full path of the file you want to edit… example:
fart D:\test\123.txt oldText newText
Thanks for your fast reply, they are really helpful. Thank you
Love your utility… I am using it as part of a project to make it easy to migrate certain web apps from one host to our CMS…
I used it to search/replace several strings in files and filenames without any problems until I tried todo this:
fart -ic *.html “http://www.domain.org/” “/”
in other words, in all the *.html files I want to replace http://www.domain.org/ with a simple forward slash. I tried this with and without the double quotes and every way I could think…
Also, after reading this blog, I tried to use the single quote by itself accidentally and so now I did replace http://www.domain.org/ with ‘/ (single quote followed by slash)… and now I can’t change this back to anything either… pretty sure this has to do with escaping special characters and such and I don’t know how with your util…
Any pointers would be helpful, thanks!
Patrick Steil
Hello Patrick,
Can you try the following please and let me know if it works… (after taking a backup!!!):
fart -iC *.html "http://www.domain.org/" \/
Note the capital C (different from the lower case c – see the command line switches in my original post) which means that C style characters are permitted in the script. This allows us to use a back slash immediately followed by a forward slash. The backslash escapes the forward slash.
Thanks for the idea, but no that didn’t work… here is what I got:
fart -iCp people.html http://www.ntcumc.org/ \/
Warning: unrecognized character escape sequence: \/
people.html
Replaced 0 occurence(s) in 1 file(s).
Also, am trying to use your tool to find and replace html tags and it is having a hard time… for example:
I want to find:
<code><a href=”Site/Map.html” rel=”nofollow”></code>
and replace with:
<code><a href=”/site/map.html” rel=”nofollow”></code>
and for example this gives me errors:
<code>fart -iC http://www.ntcumc.org\people.html <a href=”site/map.html” rel=”nofollow”> <a href=”/site/map.html” rel=”nofollow”>
< was unexpected at this time.</code>
I understand this is a command line interpreter issue, have also tried escaping the brackets and double quotes with ^ and got this:
<code>fart -iC http://www.ntcumc.org\people.html ^<a>^<a>
Error: redundant argument “<a>”.</code>
Hello Patrick,
I forgot to mention in my reply to your original comment that I did not actually create this utility π
You will need to escape the quotation marks with back slashes… like this:
fart -iC people.html "<code><a href=\"Site/Map.html\" rel=\"nofollow\"></code>" "<code><a href=\"/site/map.html\" rel=\"nofollow\"></code>"
Hello Patrick,
You forgot to use the quotation symbols around the ‘find’ clause. It should be:
fart -iCp people.html "http://www.ntcumc.org/" \/
Great utility!
Is it possible to replace chunks of text? (text with line feeds..)
Morton
Hi Morton,
You may be able to do this using quotation symbols around the text but I have a feeling it will be more difficult than this.
Let us know if you find a solution.
Hi!
I found this wonderful utility about 1month ago.
I used this utility to replace “%”to”\n”. with –c-style.
And it did worked!
But, I just reinstalled windows 7 sp1 ultimate x64.
after that, it doesn’t work.
it says Replaced 0 occurence(s) in 0 file(s).
After I enabled Verbose,
It says
C:\>fart –verbose –c-style c:\1.txt glee aaa
FART: –verbose
FART: –c-style
FART: wild_card=”c:\1.txt”
FART: find_string=”glee”
FART: replace_string=”aaa”
FART: actual find_length=4
FART: actual replace_length=3
FART: processing c:\,1.txt
FART: skipping binary file: c:\1.txt
Replaced 0 occurence(s) in 0 file(s).
Can you tell me a solution for this?URGENT!!! π
THANKS!!!
Hi Paul,
The below worked for me… perhaps you need to enclose the location of the file in quotation marks:
fart -V -C "c:\1.txt" glee aaa
Damn!!
Something screwed my windows.
Most of the command-line utility doesn’t work.
However,
looking at the verbose log,
It skips C:\s.txt
and processes c:\,s.txt.
Your solution doesn’t work for me.
But, nevermind!
I’m reinstalling Windows again…
LAST QUESTION!!
Thanks!
I’m wondering what kind of text-encoding method that FART supports?
UTF-8? UTF-16? ASCII?
Hi Park,
I have only tried to replace ‘normal’ characters – I am pretty sure it can replace text in other formats though.
Give it a try π
one more thing.
im trying to use FART to replace “/” to empty space.
if i give
FART.exe “c:\aaclog4.txt” “/” ” ”
it doesn’t works.
π
any ideas?
Park,
You need to use the -C flag for this:
fart -C βc:\aaclog4.txtβ "\/" " "
Hi,
I think I have try every combinations but I can’t replace a backslash with a simple slash !
the text is :
basedir=”C:\systran\iSystran-FrontEnd\mysql-5.5.15\”
and I would like to have
basedir=”C:/systran/iSystran-FrontEnd/mysql-5.5.15/”
1/ fart -V my.ini \ /
FART: --verbose
FART: wild_card="my.ini"
FART: find_string="\"
FART: processing ,my.ini
my.ini :
basedir="C:\\systran\\iSystran-FrontEnd\\mysql-5.5.15\\"
Nothing is replace !
2/ fart -V my.ini \ \/
FART: --verbose
FART: wild_card="my.ini"
FART: find_string="\"
FART: replace_string="\/"
FART: processing ,my.ini
my.ini
Result : basedir="C:\/\/systran\/\/iSystran-FrontEnd\/\/mysql-5.5.15\/\/" !!!
etc, etc
Have you an idea to do this simple replcement.
(I have try with -C but no more improvment !)
Hi Mathieu,
This should work for you:
fart -C file.txt \\ \/
Yeesss, you are right it works very well.
I thought I had already tested this case.
Thanks a lot.
Mathieu
I use this for MySql configuration file my.ini.
You con use only / or \\ when you put path:
datadir=”C:/mysql-5.5.15/data/
or
datadir=”C:\\mysql-5.5.15\\data\\
Hi, again just to add a little comment.
Thie command works but as you can see there is a warning.
fart -C my.txt \\ \/
Warning: unrecognized character escape sequence: \/
my.txt
Replaced 25 occurence(s) in 1 file(s).
A find a tip : echo %ERRORLEVEL% gives the number of replacements. here 25.
Yes you are right – not sure why that is. It does work though so I wouldn’t be too worried about it π
Great utility, I am wondering if I can search for a string that does not have a specific suffix. For eample, is there a way I could search for all occurences of “abc” that do not include “abc-1” i.e. could end with anything else other than “-1”?
Thanks!
Hey Paul,
I have not personally tried this but I am sure it is possible.
One way you could do it (not a very good way I admit) is to have all your possibilities in excel down one column and use that as the input to a FART batch script.
This is only doable (i.e., requires little work) if the suffix is ‘constant’… for example, -2, -3, -4, -5 etc.
Hi,
Great utility.
I have a file called prefs.js at location:
C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\yce2e09c.default
The value of yce2e09c is different in every user profile.
Can I use FART to search file prefs.js in every user profile and replace a line in that file?
The text I want to change is (“app.update.auto”, true) with (“app.update.auto”, false)
Please help.
Hello Umesh,
Have you tried using the -r switch to recursively search through directories? You would put the -r switch followed with the search path “C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\”… that way the script will search all sub-directories of \Profiles.
Hope that helps.