Search and replace text within text and binary files with the free sfk replace tool for the Windows, Mac OS X and Linux command line.
sfk replace singleFile [-text] /src/dst/ [pattern2] [...] [-yes]sfk replace -[s]pat -bin[ary] /A0A1A2/B5B6B7/ -dir anydir -file .ext1 [-yes]sfk rep [-dump [-wide]] -bylist words.txt file1 [file2 ...] [-yes]
replace text or binary data in text and binary files.
may replace many different patterns in parallel.
Multiple search patterns are executed in the given sequence. Mind this
if they overlap, e.g. /foo/bar/ /foosys/thesys/ makes no sense (foo is
replaced by the first expression, so the 2nd one will fail to match).
by default, replace functions run in SIMULATION mode,
previewing hits without changing anything. add -yes to apply changes.
Changing binaries may lead to unpredictable results, therefore keep
backups of your files in any case.
subdirectories are included by default
the sfk default for most commands is to process the given directories,
as well as all subdirs within them. specify -nosub to disable this.
options
-nosub do not include files in subdirectories.
-nobin[ary] skip binary files.
-case case-sensitive text comparison. default is case-insensitive
comparison for all -text strings, but NOT for -bin blocks.
case-sensitive comparison is faster then case-insensitive.
for further details type: sfk help nocase
-nocase force case-insensitive comparison ALSO on -bin patterns.
-pat starts a list of search or replace patterns of the form
xsrcxdstx where x is the separator char, src the source
to search for, and dst the destination to replace it with.
e.g. /foo/bar/ or _foo_bar_ both replace foo by bar.
-pat is not required if a single filename is given.
-text the same as -pat, starting a text pattern list.
-spat same as -pat but also activates slash patterns like \t .
type "sfk help pat" for the list of possible patterns.
-spats[trict] same as -spat, but stops with error on undefined
slash patterns like \m in C:\myproj. every slash
must then be escaped, e.g. using C:\\myproj.
-bin[ary] starts a list of binary replace patterns, specified
as hexcode like /0A0D/2020/
-bylist x.txt read search patterns from a file x.txt, supporting
multiple lines per pattern. (add -full for more.)
-bylinelist x read /from/to/ or just /from/ patterns from a file x
with one pattern per line. (add -full for more.)
-by(line)list does not support sfk variables.
to use variables in patterns create an sfk script
with patterns as parameters. "sfk script" for more.
-usetmp allow creation of temporary files if output data is larger
than the memory limit (default: 300 MB). without -usetmp,
SFK uses the whole RAM, but stops with an error if it runs
out of memory.
-memlimit=n use up to n mbytes of RAM to store output data, and when
the limit is reached, use a temporary file. this option
implies -usetmp. to set this permanently by environment,
type in a batch file: set SFK_CONFIG=memlimit:n
-tmpdir x set directory x as temporary file directory. default is
to use the path specified by TEMP or TMP env variable.
-showtmp tell verbosely which temporary files are created.
SFK temporary filenames contain the process ID
to make sure multiple SFK running in parallel
do not use the same temporary file.
-notmp never create temporary files (default). if combined with
-memlimit, sfk stops with an error if memlimit is reached.
-recsize set input record size for processing (default=100k).
-firsthit process only first found pattern match per file.
-maxscan=nm stop searching after (approximately) first n megabytes
per file. can be used only with same length replace.
-quiet do not show progress infos.
-stat show statistics like hits per pattern and no. of files.
-perf show performance statistics.
-full print full help text telling about -bylist pattern files,
special character case sensitivity and nested or repeated
replace behaviour.
output options
-dump create hexdump of search hits or replaced text.
-wide with -dump: show 16 bytes per line.
-lean with -dump: show 8 bytes per line.
-dumpfrom always dump search hits but not replaced text.
-dumpall dump search text and replaced text.
-nodump do not create a hexdump, list only matching files.
-astext no hexdump, but print search hits as plain text.
use this only with plain text files, not binary.
-showle highlight CR/LF line endings in hex dump output
-context=n with hexdump: show additional n bytes of context.
-reldist with hexdump: tell relative distances to previous hits.
-to dir\$file write output files to given path. for details about
output file masks, type "sfk help opt" or "sfk run".
-tofile x write output data to a single output filename x
(which is not interpreted as a mask but taken as is).
-more[n] pause output every 30 or n lines.
-showhits list matching and missing search patterns.
-showjusthit or -showmiss lists only matching or missing patterns.
return codes for batch files
0 = no matches, 1 = matches found, >1 = major error occurred.
see also "sfk help opt" on how to influence error processing.
temporary files
with option -usetmp or -memlimit sfk may create temporary files
in a folder specified by TEMP or TMP environment variable,
or within /tmp under Linux, or in a folder given by -tmpdir
or from an SFK_CONFIG=tmpdir:... setting. type "sfk help opt"
for further infos.
unexpected repeat replace behaviour
depending on the input data and search/replace expressions,
it can happen that running the same replace multiple times
on the same file produces further hits that didn't exist
in the first run. add option -full to read more on this.
quoted multi line parameters are supported in scripts
using full trim. type "sfk script" for details.
performance notice
the system may cache output file(s), writing to disk in
background after sfk has finished. subsequent batch file
commands may execute slower.
office file supportsfk ofind search in .xml text file contents of
office files like .docx .xlsx .ods .odt.
sfk help office for more infos and options
see alsosfk xfind search wildcard text in plain text files
sfk ofind search in office files .docx .xlsx .ods
sfk xfindbin search wildcard text in text/binary files
sfk xhexfind search in text/binary with hex dump output
sfk extract extract wildcard data from text/binary files
sfk filter filter and edit text with simple wildcards
sfk find search fixed text in text files
sfk findbin search fixed text in text/binary files
sfk hexfind search fixed text in binary files
sfk replace replace fixed text in text/binary files
sfk view GUI tool to search text as you type
sfk replace replace fixed text with high performance
sfk xreplace replace wildcard text in text/binary files
sfk setbytes change byte sequences at absolute position
sfk partcopy copy, split and join parts of files
beware of Shell Command Characters.
to find or replace text patterns containing spaces or special
characters like <>|!&?* you must add quotes "" around parameters
or the shell environment will destroy your command. for example,
pattern /foo bar/other/ must be written like "/foo bar/other/"
within a .bat or .cmd file the percent % must be escaped like %%
even within quotes: sfk echo -spat "percent %% is a percent \x25"common usage errorssfk hexfind in.txt "/foo\r\n/"
will not find "foo" at line ends, but searches literal
strings like "slash and r". add option -spat to enable
slash patterns, converting \r\n to real CRLF codes,
or use xhexfind where slash patterns are default.
sfk hexfind mydir "/foo*bar/"
will not find "foo" and "bar" with any characters
inbetween, but searches a literal star "*".
use xhexfind to enable search with wildcards.
sfk rep in.txt "/foo[0.1000 bytes]bar/---/"
will not replace up to 1000 bytes between "foo" and "bar",
but replaces a literal string "foo" then "[1000 bytes]"
then "bar". use xed or xreplace instead.
examplessfk replace myfile.dat "/Lemon/Curry/"
replace Lemon by Curry within myfile.dat. search is
case-insensitive, therefore "lemon" will be replaced as well.
the quotes "" are optional here.
sfk replace -pat /FooCase// -dir . -file .txt
search for FooCase in all .txt files below current directory.
note that if you leave out the single fileOrDir parameter,
you have to say -[s]pat, -bin or -text first to tell clearly
that you will specify the target fileset later on.
sfk rep -spat "/The foo/The\tbar/" -dir mydocs
replaces "The foo" by "The" and "bar" separated by TAB char,
within all files within mydocs.
sfk rep -binary /1A/20/ -dir docs -file .txt .info .note
replaces all bytes with code 0x1A by code 0x20, in all .txt,
.info and .note files, in directory docs and all subdirectories.
sfk replace tmp\image.dat -bylist patches.txt
searches for source patterns in file tmp\image.dat,
replaces by patterns specified in patches.txt
sfk rep -nosub -bin /00/01/ -dir docs -file .doc -to output\$file
replace binary 00 by 01 in all docs\*.doc files, but do not look
into deeper sub directories. write output files to folder "output".
sfk hexfind mydir -pat /FooCase/ +list -late
do not replace, just find binary files containing "FooCase",
then list them sorted by time. also accepts -bylist files.
sfk replace -binary /666f6f/626172/ -dir mydir -file .dat
replace binary data with hex values 0x66, 0x6f, 0x6f
by data with values 0x62, 0x61, 0x72 in all .dat files.
example with output:
preparing a replacement of pattern "class Foo".
sfk rep testfiles "/class foo/class Other/" -dump
output:
[simulating:][total hits/matching patterns/non-matching patterns]testfiles\FooBank\GUI\include\FooGUI.hpp: hit at offset 0x64
>6F6E616C 6974792E<onality. 00000054
>0D0A2A2F 0D0A0D0A< ..*/.... 0000005C
>636C61737320466F<class Fo 00000064
>6F475549 0D0A7B0D<oGUI..{. 0000006C
>0A707562 6C69633A< .public: 00000074
[001/1/0] testfiles\FooBank\GUI\include\FooGUI.hpp
testfiles\Formats\12-foo-jam.txt: hit at offset 0xE6C
>6F6E616C 6974792E<onality. 00000E5C
>0D0A2A2F 0D0A0D0A< ..*/.... 00000E64
>636C61737320466F<class Fo 00000E6C
>6F475549 0D0A7B0D<oGUI..{. 00000E74
>0A707562 6C69633A< .public: 00000E7C
[001/1/0] testfiles\Formats\12-foo-jam.txt
38 files checked, 2 would be changed.
[add -yes to execute.]
the input data is listed as hex and ascii, with the hits highlighted.