Last active
June 28, 2019 17:48
-
-
Save mftrhu/0083afb929c7e86ad36d0bb49e537835 to your computer and use it in GitHub Desktop.
A simple shell script to get Google search results on the terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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=' '&') | |
[ -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