Usando o Google como uma consulta do dicionário via bash, Como se pode agarrar a primeira definição?
-
06-07-2019 - |
Pergunta
#!/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 "*"
despeja toda uma série de definições de google.com um exemplo está abaixo:
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"
A coisa é, eu não quero que todas as definições, apenas o primeiro:
universe: everything that exists anywhere; "they study the evolution of the universe"; "the biggest tree in existence"
Como pode uma garra que a sentença fora da saída? Sua entre dois *, que poderia ser usado?
Solução
Este irá retirar a bala desde o início da primeira linha, imprimi-lo e descartando o resto da saída.
sed 's/^ *\* *//; q'
Outras dicas
Adicione esta:
head -n 1 -q | tail -n 1
Assim torna-se:
#!/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 cabeça tentativa
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow