Usando google como una búsqueda de diccionario a través de bash, ¿Cómo se puede tomar la primera definición?

StackOverflow https://stackoverflow.com/questions/1617152

Pregunta

#!/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 "*"

Vuelve toda una serie de definiciones de google.com. A continuación se muestra un ejemplo:

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"

Lo que pasa es que no quiero todas las definiciones, solo la primera:

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

¿Cómo se puede extraer esa frase de la salida? Está entre dos *, ¿podría usarse?

¿Fue útil?

Solución

Esto eliminará la viñeta del principio de la primera línea, imprimiéndola y descartando el resto de la salida.

sed 's/^ *\* *//; q'

Otros consejos

Añade esto:

head -n 1 -q | tail -n 1

Así se convierte en:

#!/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 -q | tail -n 1

comando head head

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top