Используя Google как поиск по словарю через bash, Как можно получить первое определение?

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

Вопрос

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

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

Это было полезно?

Решение

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

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

Другие советы

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

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top