write a find/replace script (in your fav lang) to do find replace all html files in a dir.
the script will be run in shell. it'll take 0 arg. (hardcode the input dir)
the input source is emacs 2.8 emacs lisp manual (html version, one page per node version. it's about 1k files. you can download it online.)
see which lang is fastest. (timing first run, and second run)
text pairs that we want to do find and replace.
Case matters
"Previous:" → "prev_ykzFQ"
"that" → "that_hM8xd"
"lambda" → "lam_cnj3G"
"number" → "num_j2CWg"
first run result, single file
(first run may include compile time, or loading executable to memory)
python3 : 0.17
go_compiled : 0.19
elisp : 0.39
go : 0.6
ruby : 1.28
WolframLang : 2.1
second run result, single file
go compiled : 0.04
python3 : 0.17
ruby : 0.18
elisp : 0.29
go : 0.57
WolframLang : 1.8
first run result, 1k files
go compiled : 3.5
python3 : 3.2
go : 3.7
ruby : 3.9
elisp : 5.4
WolframLang : 5.7
second run result, 1k files
go compiled : 0.5
ruby : 0.6
go : 1.0
elisp : 1.8
WolframLang : 2.6
python3 : 3.0
WolframLang
(* 2023-01-09 find replace mulitple pairs in a dir *)(* code has a bug, of adding line return to the file *)inputDir = "c:/Users/xah/xx/";
xpairs = {
"Previous:" -> "prev_ykzFQ",
"that" -> "that_hM8xd",
"lambda" -> "lam_cnj3G",
"number" -> "num_j2CWg"
};
Scan[
Module[{xold = ReadString[#], xnew},
xnew = StringReplace[xold, xpairs];
If[xold =!= xnew, WriteString[#, xnew]];
] &
,
FileNames["*html", inputDir, {1}] ]
Emacs Lisp
;; 2023-01-09 find and replace string of all files in a dir
(setqxinputDir"c:/Users/xah/xx/")
(setqxFindReplacePairs
[
["Previous:""prev_ykzFQ"]
["that""that_hM8xd"]
["lambda""lam_cnj3G"]
["number""num_j2CWg"]
])
(defun my-do-file (fPath)
"Process the file at path FPATH"
(let (($changed-pnil))
(with-temp-buffer
(insert-file-contents fPath)
(mapc
(lambda (x)
(let (($find (arefx 0)) ($rep (arefx 1)))
(goto-char 1)
(when (search-forward$findnilt)
(progn (replace-match$reptt) (setq$changed-pt)))
(while (search-forward$findnilt)
(replace-match$reptt))))
xFindReplacePairs)
(when$changed-p (write-region (point-min) (point-max) fPath)))))
(mapc #'my-do-file
(directory-filesxinputDirt"html$"t))
python
# Python 3
# find replace mulitple pairs in a dir. 2023-01-08
import sys, os, re
xinput = "c:/Users/xah/xx/"xpairs = [
("Previous:", "prev_ykzFQ"),
("that", "that_hM8xd"),
("lambda", "lam_cnj3G"),
("number", "num_j2CWg"),
]
defdoFile(xpath):
"Replace find/replace in xpairs in xpath"inputFile = open(xpath, "r", encoding="utf-8")
xMeat = inputFile.read()
inputFile.close()
xNew = xMeat
for apair in xpairs:
xNew = xNew.replace(apair[0], apair[1])
if xNew != xMeat:
xoutFile = open(xpath, "w", encoding="utf-8")
xoutFile.write(xNew)
xoutFile.close()
for dirPath, subdirList, fileList in os.walk(xinput):
for fName in fileList:
if re.search(r"\.html$", fName, re.U):
doFile(dirPath + os.sep + fName)
Ruby
# 2023-01-10
# find replace multiple pairs in a dir.
# code by george
input_dir = "c:/Users/xah/xx/"
replacements = [
["Previous:", "prev_ykzFQ"],
["that", "that_hM8xd"],
["lambda", "lam_cnj3G"],
["number", "num_j2CWg"],
]
replace = lambdado |file|
content = File.read(file)
result = replacements.reduce(content) { |string, (pattern, replacement)| string.gsub(pattern, replacement) }
File.write(file, result) if content != result
endDir.glob(File.join(input_dir, "**", "*.html"), &replace)
golang
// 2023-01-09 find replace mulitple pairs in a dir
packagemainimport (
"fmt""io/fs""os""path/filepath""regexp""strings"
)
var inDir = "c:/Users/xah/xx/"var fnameRegex = `\.html$`type frPair struct {
findStr string
replaceStr string
}
// frPairs is a slice of frPair struct.
var frPairs = []frPair{
frPair{findStr: `Previous:`, replaceStr: `prev_ykzFQ`},
frPair{findStr: `that`, replaceStr: `that_hM8xd`},
frPair{findStr: `lambda`, replaceStr: `lam_cnj3G`},
frPair{findStr: `number`, replaceStr: `num_j2CWg`},
}
func doFile(path string) error {
contentBytes, er := os.ReadFile(path)
if er != nil {
fmt.Printf("processing %v\n", path)
panic(er)
}
var content = string(contentBytes)
var changed = false
for _, pair := range frPairs {
var found = strings.Index(content, pair.findStr)
if found != -1 {
content = strings.Replace(content, pair.findStr, pair.replaceStr, -1)
changed = true
}
}
if changed {
err2 := os.WriteFile(path, []byte(content), 0644)
if err2 != nil {
panic("write file problem")
}
}
returnnil
}
var pWalker = func(xpathstring, xinfo fs.DirEntry, xerrerror) error {
ifxerr != nil {
fmt.Printf("error [%v] at a path [%q]\n", xerr, xpath)
returnxerr
}
{
varx, err = regexp.MatchString(fnameRegex, filepath.Base(xpath))
if err != nil {
panic("stupid MatchString error 59767")
}
ifx {
doFile(xpath)
}
}
returnnil
}
funcmain() {
err := filepath.WalkDir(inDir, pWalker)
if err != nil {
fmt.Printf("error walking the path %q: %v\n", inDir, err)
}
}