Want to change fonts in a whole bunch of SVG files, from Open Sans to Source Sans Pro. SVG (scaleable vector graphics files are text files, so we can use the LInux/BSD command line. Now, this here is for Linux — the BSD versions of some commands differ a little.
See: https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/
To search through one file and change all occurrences, something like:
$ sed -i.bak 's/Open Sans/Source Sans Pro/g' file.svg
should do it.
sed = stream editor (the program that does the work)
-i = inplace (change the file in place, rather than writing a new one); but create a backup with extension .bak
s = substitute (ie find and replace)
/ = field separator
Open Sans = string to find
Source Sans Pro = string to replace it with (on BSD/Mac you may need to use Open\ Sans, where the backslash ‘escapes’ the spaces and makes the program read it literally).
g = global (do it everywhere — otherwise will do only first appearance on a line)
file.svg = name of your svg file.
If they are all in the same folder, you can just go (Linux; not sure about BSD/Mac):
$ sed -i.bak 's/Open Sans/Source Sans Pro/g' *.svg
And it should do them all. If that does not work, look over them:
$ for f in *.svg ; do sed -i.bak ‘s/Open Sans/Source Sans Pro/g’ $f.svg ; done
If the svg files are in many directories, you’ll need to find them first. If you go to the root of the file Figures tree, something like:
$ find . -name "*.svg" -type f -exec sed -i .bak 's/Open Sans/Source Sans Pro/g' {} \;
find = program to find files
. = begin the search here (for example, you have first gone “cd /path/to/my/files”; can specify another directory here if you want to search elsewhere
-name = search by file name (case sensitive — -iname is insensitive)
“*.svg” = find files that end in svg (note, will NOT match .SVG; if some files have caps, use -iname)
-type f = search for files (can also search for directories, -type d)
-exec = when you find a file that matches, execute the following command
then it is as above sed command
{} = put the name of the file you found here (eg file.svg)
\; = ‘we are done’
Now, sed and find on Mac may not behave exactly as they do on Linux, which is what I use. I suggest you test the sed command on a single file first.
Here is my test file, test.txt
Open Sans
Fred fred fred Open Sans
Open SansOpen SansOpen Sans
I run the command:
$ sed -i.bak 's/Open Sans/Source Sans Pro/g' test.txt
and now this is my file:
Source Sans Pro
Fred fred fred Source Sans Pro
Source Sans ProSource Sans ProSource Sans Pro
and I also have a backup file: test.txt.bak
If you type simply:
$ find . -name "*.svg"
It will show you all the svg files that sed will operate on. Good to do before running the ‘real’ command.
You can customise the find command; for example:
$ find -name "*final*.svg"
this will only find files with lower case ‘final’ in the name and ending in svg. I can make it more complicated — say I only and files with numbers 30 to 49 in the title:
$ find . -name "*[3-4][0-9]*.svg"
./URB_45/URB_45-01-06Oct21.svg
./URB_33/URB_33-attempt1-hori2-combined.svg
./URB_33/URB_33-attempt1.svg
./URB_33/URB_33-attempt1-hori2.svg
./URB_33/URB_33-attempt1-hori.svg
etc
here [3-4][0-9] will cover character combinations 30 to 39 then 40 to 49.
On Mac
sed on Mac might need:
$ sed -i '.bak' -e 's/string1/string2/' {} \;
That is, it might be different from what worked for me. These methods will be possible, but Mac is not Linux and some fine tuning will be needed.
Thus endeth the random ramble