by shubhamjain · 4y ago · 0 comments
The only thing I wish was present was better support for RegExes. Bash and most unix tools don't support PCRE which can severely limiting. Plus, sometimes you want to process text as a whole vs line-by-line.
I would also recommend Python's sh[4] module if Shell scripting isn't your cup of tea. You get best of both worlds: faster dev work with Bash utils, and a saner syntax.
[1]: https://github.com/ericchiang/pup
[2]: https://csvkit.readthedocs.io/en/latest/
>rm -rf /usr /lib/nvidia-current/xorg/xorg
https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/commi...
>rm -rf "$STEAMROOT/"*
https://github.com/valvesoftware/steam-for-linux/issues/3671
It's just too easy to shoot your foot.
> set -euo pipefail
Here's a detail explaination of all the switches: https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e82....
I do agree though, it's not the best tool. But combining CLI utilities tends to be fast.
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. shot-scraper javascript \
"https://news.ycombinator.com/from?site=simonwillison.net" "
Array.from(document.querySelectorAll('.itemlist .athing')).map(el => {
const title = el.querySelector('a.titlelink').innerText;
const points = parseInt(el.nextSibling.querySelector('.score').innerText);
const url = el.querySelector('a.titlelink').href;
const dt = el.nextSibling.querySelector('.age').title;
const submitter = el.nextSibling.querySelector('.hnuser').innerText;
const commentsUrl = el.nextSibling.querySelector('.subtext a:last-child').href;
const id = commentsUrl.split('?id=')[1];
const numComments = parseInt(
Array.from(
el.nextSibling.querySelectorAll('.subtext a[href^=item]')
).slice(-1)[0].innerText.split()[0]
) || 0;
return {id, title, url, dt, points, submitter, commentsUrl, numComments};
})
" | jq '. | map(.numComments) | add'
That example scrapes a page on Hacker News by running JavaScript inside headless Chromium, outputs the results as JSON to stdout, then pipes them into jq to add them up. It outputs "1274".https://simonwillison.net/2022/Mar/14/scraping-web-pages-sho...
(Fun side note: I figured out the jq recipe I'm using in this example using GPT-3: https://til.simonwillison.net/gpt3/jq )