Skip to content

Instantly share code, notes, and snippets.

@wickkidd
Created March 19, 2019 15:23
Show Gist options
  • Save wickkidd/7d9c87d7459b58f1e0a6df71906cb234 to your computer and use it in GitHub Desktop.
Save wickkidd/7d9c87d7459b58f1e0a6df71906cb234 to your computer and use it in GitHub Desktop.
I wanted to replace string matches with the output of piped shell commands
# python
import subprocess
import re
o = open("some_html_file.out.html","w")
data = open("./some_html_file.html").read()
def gen_id(match):
match = match.group()
id = subprocess.getoutput("cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 4")
return f'{match} id="{id}"'
o.write(re.sub(r'(<p)', gen_id, data))
o.close()
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment