Using google as a dictionary search through bash, how can I capture the first definition?

#!/bin/bash
# Command line look up using Google 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 "*"

Dumps a series of definitions from google.com below:

Type in your word:
world
    * universe: everything that exists anywhere; "they study the evolution of the universe"; "the biggest tree in existence"
    * people in general; especially a distinctive group of people with some shared interest; "the Western world"
    * all of your experiences that determine how things appear to you; "his world was shattered"; "we live in different worlds"; "for them demons were as much a part of reality as trees were"

The thing is, I do not want all the definitions, only the first:

universe: everything that exists anywhere; "they study the evolution of the universe"; "the biggest tree in existence"

How can I get this sentence out of the conclusion? Can it be used between two *,

+3
source share
3 answers

This will remove the bullet from the beginning of the first line, print it and discard the rest of the output.

sed 's/^ *\* *//; q'
+2
source

Add this:

head -n 1 -q | tail -n 1

So it will be:

#!/bin/bash
# Command line look up using Google 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 -q | tail -n 1
+1
source

0

Source: https://habr.com/ru/post/1720952/


All Articles