Используя Google как поиск по словарю через bash, Как можно получить первое определение? - PullRequest
3 голосов
/ 24 октября 2009
#!/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 "*"

Сбрасывает целую серию определений с google.com, пример ниже:

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"

Дело в том, что мне не нужны все определения, только первое:

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

Как извлечь это предложение из вывода? Это между двумя *, это может быть использовано?

Ответы [ 3 ]

2 голосов
/ 24 октября 2009

Это уберет маркер с начала первой строки, напечатает его и отбросит остальную часть вывода.

sed 's/^ *\* *//; q'
1 голос
/ 24 октября 2009

Добавить это:

head -n 1 -q | tail -n 1

Так становится:

#!/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
0 голосов
/ 24 октября 2009

Попробуй команду

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...