Skip to content

Instantly share code, notes, and snippets.

@ezr
Created September 21, 2021 02:27
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 ezr/840aeccffb9739cda32fff2caec57f96 to your computer and use it in GitHub Desktop.
Save ezr/840aeccffb9739cda32fff2caec57f96 to your computer and use it in GitHub Desktop.
Print the supplied template once for each line of stdin
#!/usr/bin/env python
import sys
if len(sys.argv) == 1:
text = "{text}"
message = '''\
Error: no argument supplied.
This program prints the template once for each line of stdin. {text} in the template is replaced with the line from stdin.
Usage:
{argv0} <template> < data
Examples:
cat names.txt | {argv0} 'Hello {text}. Goodbye {text}!'
{argv0} 'ping -c 1 {text}' < hosts.txt'''
print(message.format(text=text, argv0=sys.argv[0]))
sys.exit(1)
for line in sys.stdin.readlines():
print(sys.argv[1].format(text=line.strip()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment