使用谷歌作为字典查找通过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"
如何从输出中获取该句子?它在两个*之间,可以用吗?
解决方案
这将从第一行的开头剥离子弹,打印它并丢弃剩余的输出。
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
尝试头命令
不隶属于 StackOverflow