3 ms·
For things like regular expressions, it's useful to know that Python has a "-c" option which can be passed a multi-line string as part of a CLI pipeline. You ca
by simonw 4y ago
For things like regular expressions, it's useful to know that Python has a "-c" option which can be passed a multi-line string as part of a CLI pipeline. You can do something like this:
curl 'https://news.ycombinator.com/' | python -c '
import sys, re, json
html = sys.stdin.read()
r = re.compile("<a href=\"(.*)\"")
print(json.dumps(r.findall(html), indent=2))
'
This outputs JSON which you can then pipe to other tools.
- shubhamjain 4y agoThis is great. perl also has one-liners [1] one can use, but I gave up dealing with perl's obscure syntax. This is much better. [1]: http://novosial.org/perl/one-liner/ http://novosial.org/perl/one-liner/
- infinite8s 4y agoThat's great! I didn't know -c supported multiline - I always just crammed it into one line with semicolons.
- simonw 4y agoYeah I was the same - I only figured out the multi line trick a few days ago https://til.simonwillison.net/aws/boto-command-line https://til.simonwillison.net/aws/boto-command-line