Googleをbash経由の辞書検索として使用して、最初の定義を取得するにはどうすればよいですか?
-
06-07-2019 - |
質問
#!/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"
どうすれば出力からその文を取り出すことができますか?その2つの*の間に使用できますか?
解決
これにより、最初の行の先頭から行頭文字が削除され、印刷されて残りの出力が破棄されます。
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コマンドを試す
所属していません StackOverflow