Linux 通过bash使用google作为字典查询,如何获取第一个定义?
从google.com转储一系列定义示例如下:Linux 通过bash使用google作为字典查询,如何获取第一个定义?,linux,command-line,dictionary,lookup,Linux,Command Line,Dictionary,Lookup,从google.com转储一系列定义示例如下: #!/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
#!/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 "*"
问题是,我不想要所有的定义,只想要第一个:
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"
尝试使用head命令添加以下内容:
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