Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Search sites via Google and Terminal Internet
Here is a script that allows you to Google your query by typing it into Terminal:
#!/bin/sh

read F1
F=`echo $F1 | sed s/\ /+/g`

open "http://google.com/search?q=$F"
Copy and paste that into a new blank document in vi, pico, emacs, etc., and save it somewhere on your user's path. Then make it executable (chmod a+x googleit) and you can run it by typing googleit (or whatever you called it), pressing Return, and then entering your search term(s). You can't do a phrase search with this one -- try something quoted like "steve jobs and bill gates" and it will fail -- but I am hoping that someone here can come up with a solution.

There is more. I usually find myself googling something inside a website. The websites that have the answer are mostly limited (at least in my case), and that makes it a drag to type the same long web address into Google's search field every time I want to search for something. So I came up with a solution...

Here is a good example which looks up word(s) in Webster via Google:
#!/bin/sh

echo "type your word(s) to look up"
read F1
F=`echo $F1 | sed s/\ /+/g`

open "http://google.com/search?q=%20site:merriam-webster.com%20$F"

Here is another one to dig deep into wikipedia via Google:
#!/bin/sh

echo "type the query that you want to know about?"

read F1
F=`echo $F1 | sed s/\ /+/g`

open "http://www.google.com/search?as_sitesearch=en.wikipedia.org&fulltext=Search&=Google&q=$F"

Search macosxhints.com via Google:
#!/bin/sh

read F1
F=`echo $F1 | sed s/\ /+/g`

open http://google.com/search?q=%20site:macosxhints.com%20$F

Search the man pages at Apple.com via Google:
#!/bin/sh

read F1
F=`echo $F1 | sed s/\ /+/g`

open http://google.com/search?q=%20site:apple.com%20manual+page+for+$F

Search Apple's developer's website via Google:
#!/bin/sh

read F1
F=`echo $F1 | sed s/\ /+/g`

open "http://developer.apple.com/cgi-bin/search.pl?q=$F&num=10&site=default_collection"

Search Apple's support page via Google:
#!/bin/sh

read F1
F=`echo $F1 | sed s/\ /+/g`

open "http://search.info.apple.com/?q=$F&type=&search=Search&lr=lang_en&search=Go"

And finally, search Google Maps via Terminal:
#!/bin/sh

echo "where to?"
read F1
F=`echo $F1 | sed s/\ /+/g`

open "http://maps.google.com/maps?f=q&hl=en&q=$F"
Special thanks to James Reynolds for help with these scripts...
    •    
  • Currently 2.13 / 5
  You rated: 4 / 5 (8 votes cast)
 
[12,444 views]  

Search sites via Google and Terminal | 14 comments | Create New Account
Click here to return to the 'Search sites via Google and Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Search sites via Google and Terminal
Authored by: rwmitchell on Jun 06, '07 07:56:59AM
I changed the code to:

#!/bin/sh

read F1
F=`echo $F1 | sed 's/ /+/g' | sed 's/"/%22/g'`

open "http://google.com/search?q=$F"
and that seemed to handle the quoting problem mentioned in the original hit.

[ Reply to This | # ]
Much easier...
Authored by: dbs on Jun 06, '07 08:23:50AM

I'm getting kind of tired of posting these same comments, but there are much better ways to do this:

1) Install LaunchBar ($cheap), Butler (free), or QuickSilver (free). I use QuickSilver, although it's more complicated to configure than LaunchBar.

2) Type: command+space go tab. Type in the words. Hit return. Done.

The reason this is so much better is that they come configured for most things you might want to search. Want to search IMDB? Or wikipedia? Same thing:

Type command+space imd tab. Type in movie title. Hit return. Done.
Type command+space wiki tab. Type in query. Hit return. Done.

You don't have to write shell scripts or apple scripts or click or, most importantly, remember the names of commands.

They're also very flexible: I wanted to search the Stanford mediatext library for DVDs. This isn't configured by default, but all I had to do was make a bookmark in Safari with *** in place of the query string and now I can type: command+space media tab. Type in DVD title. Hit return. And I get all the DVDs with that title.

The scripts in this hint are useful if you want to learn how to write shell scripts, though!



[ Reply to This | # ]
Much easier...
Authored by: macavenger on Jun 06, '07 09:07:59AM

Course, the problem with those solutions is that they don't work in the terminal :)

---
iMac FP 17" 800MHz OS X 10.4.x



[ Reply to This | # ]
Much easier...
Authored by: deef on Jun 06, '07 09:19:12AM
Or use <a href="http://free.abracode.com/cmworkshop/">OnMyCommand</a>; which has scripts for most of these searches & lets you attach terminal scripts to context menus if you need to add functionality.

[ Reply to This | # ]
Not Much easier...
Authored by: pwharff on Jun 06, '07 11:01:05AM

There is some truth to what you are saying, however, there are some like myself that spend a lot of their day in the Terminal and it is much easier to just type "G" + tab (I named my script "Google" and it is the only script/binary on my system that starts with a capital G) and type my search.



[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: deef on Jun 06, '07 09:15:03AM
If you don't ever want to leave the terminal you could install lynx then replace all the "open" commands in the scripts with "lynx". Especially useful for google maps :)

[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: pwharff on Jun 06, '07 10:53:59AM
I added the "-n" switch to make things look a little more tidy.
#!/bin/sh

echo -n "Seach Google for: "

read F1
F=`echo $F1 | sed 's/ /+/g' | sed 's/"/%22/g'`

open "http://google.com/search?q=$F"


[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: Mike Dillingham on Jun 06, '07 10:55:15AM
I would like to point out that Sveinbjorn Thordarson has an excellent set of utilites called osxutils on his site. These contain two scripts, one which does google searches and the other wiki searches. You may want to check these out for ideas.

[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: pamon on Jun 06, '07 12:44:17PM
Thanks to rwmitchell here is updated version of all the codes. I have also changed the macosxhints.com part quite a bit. Google
#!/bin/sh

read F1
F=`echo $F1 | sed s/\ /+/g | sed 's/"/%22/g'`

open "http://google.com/search?q=$F"

Webster
#!/bin/sh

read F1
F=`echo $F1 | sed 's/\ /+/g' | sed 's/"/%22/g'`
open "http://google.com/search?q=%20site:merriam-webster.com%20$F"

Wikipedia
#!/bin/sh

read F1
F=`echo $F1 | sed 's/\ /+/g' | sed 's/"/%22/g'`

open "http://google.com/custom?hl=en&cof=&domains=en.wikipedia.org&btnG=Search&sitesearch=en.wikipedia.org&fulltext&q=$F"

macosxhints.com
#!/bin/sh

read F1
F=`echo $F1 | sed s/\ /+/g | sed 's/"/%22/g'`

open "http://google.com/custom?hl=en&cof=&domains=www.macosxhints.com&btnG=Search&sitesearch=www.macosxhints.com&q=$F"

man pages on apple.com
#!/bin/sh

read F1
F=`echo $F1 | sed 's/\ /+/g' | sed 's/"/%22/g'`

open http://google.com/search?q=%20site:apple.com%20manual+page+for+$F

apple's developer's website
#!/bin/sh

read F1
F=`echo $F1 | sed 's/\ /+/g' | sed 's/"/%22/g'`

open "http://developer.apple.com/cgi-bin/search.pl?q=$F&num=10&site=default_collection"

apple's support page:
#!/bin/sh

read F1
F=`echo $F1 | sed 's/\ /+/g' | sed 's/"/%22/g'`

open "http://search.info.apple.com/?q=$F&type=&search=Search&lr=lang_en&search=Go"

Google Maps:
#!/bin/sh

echo "where to?"
read F1
F=`echo $F1 | sed 's/\ /+/g' | sed 's/"/%22/g'`

open "http://maps.google.com/maps?f=q&hl=en&q=$F"


[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: pwharff on Jun 06, '07 01:07:30PM

It would be much easier to use special variables to accept options and arguments in combination with CASE to combine all these search engines into one script.



[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: pwharff on Jun 06, '07 01:03:39PM
It's also much easier if you provide an argument to the script, so that you can perform this much faster and easier (especially if you put this script in one of your PATH's).

#!/bin/sh

if [ $# = 0 ]
then
  echo " "
  echo "Please provide search string. Usage: $0 [string]"
  echo " "
else
  F=`echo $@ | sed 's/ /+/g' | sed 's/"/%22/g'`
  open "http://google.com/search?q=$F"
fi


[ Reply to This | # ]
Version with full quoting/punctuation support
Authored by: jvinocur on Jun 06, '07 04:19:15PM
I've long been using the code below with lynx for quick command-line based searches. It takes search terms as command-line arguments, handles quoting, and correctly escapes a wide range of punctuation (since things like apostrophes are not permitted in URLs).

#!/usr/bin/perl -w
use strict;

# Original by Ilmari Karonen in <978603463.22920@itz.pp.sci.fi>
# Modifications by Jeffrey M. Vinocur

my $u = 'http://www.google.com/';
if (@ARGV) {
  foreach (@ARGV) {
    s/[^ \w]/sprintf '%%%02X', ord $&/eg;  # escape non-alphanum
    if( s/ /+/g ) { $_ = "\"$_\"" };
  }
  $u .= "search?q=" . (join '+', @ARGV);
}

exec 'open', $u;


[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: johnga1t on Jun 11, '07 08:31:03AM
agreed that passing arguments (as opposed to waiting for stdin) is the way to go. this version supports quoting and has an extra for a "feelin lucky" switch, so that
 goog -l "this that" and the "other thing" 
will send you straight to the page for the first hit.

really getopts should be used with a switch to specify a particular site to search.

#!/bin/bash

google="http://www.google.com/search?"

if [ $# -lt 1 ]
    then
    scriptname=`basename $0`
    echo ""
    echo "usage: $scriptname [-l] keyword1 [keyword2 [\"key word 3\"]]" >&2
    echo ""
    exit 1
fi

# if the argument -l is passed, use the "feelin lucky" feature
# and go to first hit
if [ "$1" == "-l" ]
    then
    google=$(echo $google"btnI=I%27m+Feeling+Lucky&")

    shift
fi
google=$(echo $google"q=")


for i 
do
  # get number of words in argument
  numwords=`echo "$i" | wc -w`
  if [ $numwords -gt 1 ];
      then
      # more that one word -> quote and replace spaces w/ +
      arg=%22${i// /+}%22
  else
      arg=$i
  fi
  # add to search string
  google=$google$arg+
done
# remove trailing +
google=`echo -n $google | sed -e 's/\+$//'`

# open browser w/ search link
open "$google"
all that being said, i agree with the above that there are better ways to do these things. especially with firefox keyword searches.

[ Reply to This | # ]
Search sites via Google and Terminal
Authored by: pamon on Jun 14, '07 05:39:16AM
Google's Keyboard shortcuts
#!/bin/sh

read F1
F=`echo $F1 | sed s/ /+/g | sed 's/"/%22/g' | sed 's/&/%26/g'`

open "http://google.com/search?q=$F&esrch=BetaShortcuts"


[ Reply to This | # ]