通过 bash 使用 google 作为字典查找,如何获取第一个定义?

发布于 2024-08-09 08:07:13 字数 1022 浏览 3 评论 0原文

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

如何从输出中抓取该句子?两个*之间可以用吗?

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

Dumps a whole series of definitions from google.com an example is below:

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"

Thing is, I don't want all the definitions, just the first one:

universe: everything that exists anywhere; "they study the evolution of the universe"; "the biggest tree in existence"

How can a grab that sentence out from the output? Its between two *, could that be used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌无敌 2024-08-16 08:07:13

这将从第一行的开头剥离项目符号,打印它并丢弃其余的输出。

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

This will strip the bullet from the beginning of the first line, printing it and discarding the rest of the output.

sed 's/^ *\* *//; q'
深海里的那抹蓝 2024-08-16 08:07:13

添加这个:

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

Add this:

head -n 1 -q | tail -n 1

So it becomes:

#!/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
江心雾 2024-08-16 08:07:13

尝试 head 命令

try head command

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文