Bash script to define words

Discussion in 'Mac Programming' started by noahgolm, Dec 7, 2010.

  1. noahgolm macrumors member

    Joined:
    Aug 18, 2010
    #1
    Hi, I;m trying to make a simple bash script to google the definition of a word (http://google.com/search?q=define:), and I want it to display the results in text, or even just the first result. So far, it'll just open up the search results page in my browser. Here's the script so far:
    Code:
    echo 'Enter a word to define: '
    
    read F1
    F=`echo $F1 | sed s/\ /+/g`
    
    open "http://google.com/search?q=define:$F"
    
    
    Any suggestions on reading the html, accessing the right words, and displaying it as plain text?
     
  2. noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #2
    Never mind, found some helpful advice elsewhere, but you do need html2text from macports...
    Anyways, here's the code:
    Code:
    #!/bin/bash
    # Command line look up using Google's define feature - command line dictionary
    
    echo "Type in your word:"
    read word
    
    /usr/bin/curl -s -A 'Mozilla/4.0'  'http://www.google.com/search?q=define%3A+'$word \
    | html2text -ascii -nobs -style compact -width 500 | grep "*" | head -n 1 | tail -n 1
    
     
  3. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #3
    I would look into the Google Search AJAX API which you can actually access as a RESTFul API. You can get a nice JSON formatted top 8 results from this.
     
  4. Dalton63841 macrumors 65816

    Dalton63841

    Joined:
    Nov 27, 2010
    Location:
    SEMO, USA
    #4
    Don't hurt me too bad for this, as I've never done anything quite like this, but if you are on a Mac, wouldn't it be easier to have the script make a call to the Dictionary app, instead of Google?
     

Share This Page