Trick: Googling from the command line

From SHellium Wiki
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

Below is a script that provides a subset of surfraw-functionality:


Okay, sometimes you need to get a URL quickly, but you forgot it. Rather than firing up the browser of your choice, you can just use this script:

google () {
echo ""
tmp1="$HOME/tmp1"
tmp2="$HOME/tmp2"
lynx -dump -width 200 "http://www.google.com/search?q=$*" > "$tmp1"
crop=$(nl "$tmp1" | grep "Search Results" | awk '{print $1}')
cat "$tmp1" | sed '/^$/d' |  tail -n +"$crop" > "$tmp2"
i=1
j=1
cmp=''
while [ "$j" -le "5" ]; do
        mystr=$(cat "$tmp2" |  grep -e "^    "$j"\." | sed -e "s/[0-9]\. ..../"$j"\. /" | sed 's/^[ \t]*//')
        [[ -z "$mystr" ]] && mystr=$(cat "$tmp2" |  grep -e "^   "$j"\." | sed -e "s/[0-9][0-9]\. ..../"$j"\. /" | sed 's/^[ \t]*//')
        cmp=$(echo "$mystr" | grep "Image results")
        if [[ -z "$cmp" ]]; then
                echo -n "      "
                echo "$mystr"
                echo -n "      "
                cat "$tmp2" | egrep "^       (http*|[a-z]*\.)[a-z]*" | head -n "$i" | tail -n 1 | awk '{print $1}'
                j=$[$j+1]
                echo ""
        fi
        i=$[$i+1]
        cmp=''
        mystr=''
done
rm "$tmp2"
rm "$tmp1"
}

Append this to your .bashrc file. Now you can use google like:

google debian howto ssh

Note: For changes in .bashrc to take effect your bash session needs to be restarted. Since you often don't want to do that, you need to run this command instead for changes in .bashrc to take effect instantly:

source ~/.bashrc


If you want to be able to run it in irssi, save this:

#!/bin/bash
echo ""
tmp1="$HOME/tmp1"
tmp2="$HOME/tmp2"
lynx -dump -width 200 "http://www.google.com/search?q=$*" > "$tmp1"
crop=$(nl "$tmp1" | grep "Search Results" | awk '{print $1}')
cat "$tmp1" | sed '/^$/d' |  tail -n +"$crop" > "$tmp2"
i=1
j=1
cmp=''
while [ "$j" -le "5" ]; do
        mystr=$(cat "$tmp2" |  grep -e "^    "$j"\." | sed -e "s/[0-9]\. ..../"$j"\. /" | sed 's/^[ \t]*//')
        [[ -z "$mystr" ]] && mystr=$(cat "$tmp2" |  grep -e "^   "$j"\." | sed -e "s/[0-9][0-9]\. ..../"$j"\. /" | sed 's/^[ \t]*//')
        cmp=$(echo "$mystr" | grep "Image results")
        if [[ -z "$cmp" ]]; then
                echo -n "      "
                echo "$mystr"
                echo -n "      "
                cat "$tmp2" | egrep "^       (http*|[a-z]*\.)[a-z]*" | head -n "$i" | tail -n 1 | awk '{print $1}'
                j=$[$j+1]
                echo ""
        fi
        i=$[$i+1]
        cmp=''
        mystr=''
done
rm "$tmp2"
rm "$tmp1"

To 'google'. Change the file's permissions to 755:

$chmod 755 google

Now in irssi do:

[(status)] /alias google /exec ./google $*

Now you can just do:

[#shellium]/google what is bash
17:58:06 < xxx> bye ya'll
17:58:08 -!- xxxxx [~xxxxx@xxx.net] has joined #shellium
17:58:10 < xxxx> u and me both brutha ;)
17:58:20 
17:58:21       1. Bash - Wikipedia, the free encyclopedia
17:58:21       en.wikipedia.org/wiki/Bash
17:58:21 
17:58:21       2. Bash Reference Manual
17:58:21       www.gnu.org/software/bash/manual/bashref.html
17:58:21 
17:58:21       3. What is Bash? - Bash Reference Manual
17:58:21       www.gnu.org/software/bash/.../What-is-Bash_003f.html
17:58:21 
17:58:21       4. What is Bash?
17:58:21       tldp.org/HOWTO/Bash-Prompt-HOWTO/x143.html
17:58:21 
17:58:21       5. Bash Reference Manual - What is Bash?
17:58:21       theory.uwinnipeg.ca/localfiles/infofiles/bash/bashref_2.html
17:58:21 
17:58:24 < xxxx> xxx hahahah that reminds me of a terry pratchet novel
Personal tools
Namespaces

Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox