Skip to content

Instantly share code, notes, and snippets.

@mftrhu
Last active June 28, 2019 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mftrhu/0083afb929c7e86ad36d0bb49e537835 to your computer and use it in GitHub Desktop.
Save mftrhu/0083afb929c7e86ad36d0bb49e537835 to your computer and use it in GitHub Desktop.
A simple shell script to get Google search results on the terminal
#! /bin/sh
split () {
awk -F "$1" "{print \$$2}" -
}
between () {
split "$1" 2 | split "$2" 1
}
bold () {
sed -r -e "s|<b>|$(tput bold)|g" \
-e "s|</b>|$(tput sgr0)|g"
}
strip_html () {
sed -r -e 's|</?[a-z]+>||g'
}
search_url="https://encrypted.google.com/search?q="
user_agent="Lynx/2.8.9dev.8 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS"
download () {
echo "${search_url}$@" | \
wget --user-agent="${user_agent}" -q -O- -i- | \
tr '\n' ' ' | \
sed -r -e 's|</a>|</a>\n|g' -e 's|[ \t]+| |g'
}
counter=0 download "$@" | \
while read -r line; do
line=$(printf "%s" "$line" | between '<p><a href="/' '<table>')
url=$(printf "%s" "$line" | between '?q=' '&amp;')
[ -z "$url" ] && continue
counter=$((counter + 1))
txt=$(printf "%s" "$line" | between '">' '</a' | bold | strip_html)
printf "[%3d] %s\n <%s>\n" "$counter" "$txt" "$url"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment