Created
September 21, 2021 02:27
-
-
Save ezr/840aeccffb9739cda32fff2caec57f96 to your computer and use it in GitHub Desktop.
Print the supplied template once for each line of stdin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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