I have a long list of URLs. Contained within each of these webpages, there are links I need to extract. The Automator action Get Link URLs from Webpages is a very useful action for this task. Unfortunately Automator itself does not handle heavy workloads very well and very often either crashes or hangs indefinitely. How might I go about this using Bash via the Mac OS X Terminal app?
Edit - this is the current script as it stands right now.
#!/bin/bash
echo "Enter up to 3 words"
read -p "" v1 v2 v3
web="$HOME/web.txt"
tmp="$HOME/tmp.txt"
err="$HOME/err.txt"
fin="$HOME/fin.txt"
arc="$HOME/arc.txt"
n="$(awk 'END {print NR}' "$web")"
echo "Processing $n URLs..."
grep 'http' "$web" | \
while read -r url; do
lynx -nonumbers -hiddenlinks=merge -dump -listonly "$url" 2>>"$err" | awk '!a[$0]++' >> "$tmp"
sleep 1
n=$((n-1))
[[ $n -gt 0 ]] && echo "$n URLs left to process..." || echo "Processing Completed!"
done
grep -e "$v1" -e "$v2" -e "$v3" "$tmp" | sort -u | cat > "$fin"
cat "$fin" >> "$arc"
for r in "Results This Session"; do echo "$(cat "$fin" | wc -l)" "$r"; done
for a in "URL's Archived"; do echo "$(cat "$arc" | wc -l)" "$a"; done
I added read -p
to the beginning of the script. Are there limitations on the number of variables that can be used like this? I've successfully used up to 9 in testing. And is there a more practical way to write this? I tried read -p "" {v1..v9}
which didn't work. I added a couple for
loops at the end to indicate how much data was processed.
Current issues
sometimes I get an error
sort: string comparison failed: Illegal byte sequence sort: Set LC_ALL='C' to work around the problem.
however when add
LS_ALL=C
to the script it doesn't seem correct this.