Using Bash to cURL a website and grep for keywords
Here's how I would do it:
#!/bin/bashkeywords="$(<./keywords.txt)"while IFS= read -r url; do curl -L -s "$url" | grep -ioF "$keywords" | while IFS= read -r keyword; do echo "$url: $keyword" donedone < ./url_list.txt
What did I change:
- I used
$(<./keywords.txt)
to read thekeywords.txt
. This does not rely on an external program (cat
in your original script). - I changed the
for
loop that loops over the url list, into awhile
loop. This guarentees that we useΘ(1)
memory (i.e. we don't have to load the entire url list in memory). - I remove /dev/null from grep.
grep
ing from/dev/null
alone is meaningless, since it will find nothing there. Instead, I invokegrep
with no arguments so that it filters itsstdin
(which happens to be the output ofcurl
in this case). - I added the
-o
flag forgrep
so that it outputs only the matched keyword. - I removed the subshell where you were capturing the output of
curl
. Instead I run the command directly and feed its output to a while loop. This is necessary because we might get more than keyword match per url.