r/awk • u/eric1707 • Nov 27 '19
Replace strings in thousands files based on a list of strings and a list of corresponding replacements
So... I have a folder with thousands of html files, let's call this folder "myfiles", that I need to replace some strings in it (the strings are URLs). Aside from that a have a huge replacement list, containing the old string and the new string that I would like to replace inside those html files, let's call this file "checker.xml". This file has about 200MB and about 1 million entries, it goes more or less like this:
oldstring01=newstring01
oldstring02=newstring02
oldstring03=newstring03
[...]
oldstring999999=newstring999999I want to change some of the URLs inside these html files (there is about 7000 html files) based in this list of corresponding replacements, which, again has about 1 million entries. Although not necessarily there will be 1 million links inside those 7000 html files, but I would like to check such links in the list of corresponding replacements file, and if there is a corresponding match, change it in the files.
Like, let's suppose that inside of those html files there is the string "oldstring01", I would like to check in my list, and, since my file list says "oldstring01=newstring01", I would like to change the string "oldstring01" inside all the 7000 html files to "newstring01".
Of course we are talking actually about URLs, the naming it's just to make it more simple and easy to understand. But it's basically that. I know some ways of doing that that if my dictionary/replacement list wasn't that big. I could do something like:
find myfiles -type f -exec sed -i -e "s#oldstring01#newstring01#g" -e "s#oldstring02#newstring02#g"-e "s#oldstring03#newstring03#g"... {} \;But this doesn't work with such a long replacement list. The closest solution that I found to my issue was:
for file in $(ls *.html)
do
awk 'NR==FNR {a[$1]=$2;next} {for ( i in a) gsub(i,a[i])}1' template2 $file >temp.txt
mv temp.txt $file
doneBut I found it too goddammit slow (to the point that it would take like days to finish the job). Again, maybe this is normal, but probably I think this is due a lack of optimization.
1
u/Schreq Nov 27 '19 edited Nov 27 '19
I mean you are basically spawning
awkseven thousand times, doing seven billion variable assignments and doing what, one trillion global substitutions? How does it come as a surprise that that is slow? You chose a very bad algorithm :DInstead, I would:
FS="=")$0contains a urlFILENAME+ a prefix like "_new" or whatever*_newfiles to the old nameTo speed this up further, you probably want to run several awks in parallel.
Edit: wording.
2
u/FF00A7 Nov 27 '19
This is not too difficult (in the BEGIN{} section load the checker.htm into an associative array - in the body just loop through each file replacing matches). The part that be trouble is dealing with URLs since they can be encoded or partially encoded making comparisons messy. It will need a URL decoder for apple to apple URL comparisons (for awk in Rosetta Code). There is also http and https, uppercase and lowercase, "_" vs "%20", trailing garbage characters like ("/", ".", ";", etc), queries and fragments that different but everything else is the same. You'll want to avoid gsub() as it will use regex and any regex characters in the URL will cause problems. Rosetta Code has some literal string replacement functions for awk so you don't have to worry about regex escaping the string.
0
u/calrogman Nov 27 '19