Skip to content

Instantly share code, notes, and snippets.

@haggen
Created March 17, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haggen/d332e0c47cf37114472edee1710bcf3c to your computer and use it in GitHub Desktop.
Save haggen/d332e0c47cf37114472edee1710bcf3c to your computer and use it in GitHub Desktop.
Randomize id attributes in a svg (or other markup)
#!/bin/bash
set -euo pipefail
help() {
cat <<-EOF >&2
Usage:
$0 [-h|-v|-i ...] <path>
Options:
-v|--verbose Print replacements.
-i|--ignore ... Comma separated list of IDs to ignore.
EOF
}
if test -z "$*"; then
help
exit 1
fi
options=$(getopt -n "$0" -o hvi: -l help,verbose,ignore -- "$@")
if test $? -ne 0; then
exit 1
fi
eval set -- "$options"
ignore=""
verbose=""
while :; do
case "$1" in
-i|--ignore)
ignore="$2"
shift
shift
;;
-v|--verbose)
verbose="yes"
shift
;;
-h|--help)
help
exit
;;
--)
shift
break
;;
esac
done
grep -Po 'id="\K[^"]+' $1 | while read id; do
if echo ",$ignore," | grep -q ",$id,"; then
test -n "$verbose" && echo "Ignoring '$id'"
continue
fi
replacement="id$(openssl rand -hex 2)"
test -n "$verbose" && echo "Replacing '$id' with '$replacement'"
sed -i "s/$id/$replacement/g" $1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment