通过 mysql 模板进行 grep 搜索

发布于 2024-10-28 09:05:16 字数 1405 浏览 1 评论 0原文

我正在尝试通过 grep 在 filenmae 中搜索,但我遇到了一些问题..

我想通过模板搜索

'apple banana'

Find rows that contain at least one of the two words.

'+apple +juice'

Find rows that contain both words.

'+apple macintosh'

Find rows that contain the word “apple”, but rank rows higher if they also contain “macintosh”.

'+apple -macintosh'

Find rows that contain the word “apple” but not “macintosh”.

'+apple ~macintosh'

Find rows that contain the word “apple”, but if the row also contains the word “macintosh”, rate it lower than if row does not. This is “softer” than a search for '+apple -macintosh', for which the presence of “macintosh” causes the row not to be returned at all.

'+apple +(>turnover <strudel)'

Find rows that contain the words “apple” and “turnover”, or “apple” and “strudel” (in any order), but rank “apple turnover” higher than “apple strudel”.

'apple*'

Find rows that contain words such as “apple”, “apples”, “applesauce”, or “applet”.

'"some words"'

Find rows that contain the exact phrase “some words” (for example, rows that contain “some words of wisdom” but not “some noise words”). Note that the “"” characters that enclose the phrase are operator characters that delimit the phrase. They are not the quotation marks that enclose the search string itself.

第一条规则: grep "apple\|banana" filename

其他规则导致问题。 请帮助我,抱歉英语不好。

I'm trying search in filenmae by grep, but I have a some problem..

I want search by templates

'apple banana'

Find rows that contain at least one of the two words.

'+apple +juice'

Find rows that contain both words.

'+apple macintosh'

Find rows that contain the word “apple”, but rank rows higher if they also contain “macintosh”.

'+apple -macintosh'

Find rows that contain the word “apple” but not “macintosh”.

'+apple ~macintosh'

Find rows that contain the word “apple”, but if the row also contains the word “macintosh”, rate it lower than if row does not. This is “softer” than a search for '+apple -macintosh', for which the presence of “macintosh” causes the row not to be returned at all.

'+apple +(>turnover <strudel)'

Find rows that contain the words “apple” and “turnover”, or “apple” and “strudel” (in any order), but rank “apple turnover” higher than “apple strudel”.

'apple*'

Find rows that contain words such as “apple”, “apples”, “applesauce”, or “applet”.

'"some words"'

Find rows that contain the exact phrase “some words” (for example, rows that contain “some words of wisdom” but not “some noise words”). Note that the “"” characters that enclose the phrase are operator characters that delimit the phrase. They are not the quotation marks that enclose the search string itself.

First rule:
grep "apple\|banana" filename

Other rules are causing problems.
Help me pls, sorry for bad english.

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

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

发布评论

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

评论(1

川水往事 2024-11-04 09:05:16

您无法使用 grep 进行评级,因此这些示例需要进一步的逻辑。其他示例可以使用 or 运算符以及 ?、* 和 + 限定符来解决。以下是您需要的正则表达式的几个示例:

wesbailey@feynman:~/tmp> cat file.txt 
apple strudel
apple other txt
apple macintosh other text
macintosh other txt
other text strudel

查找至少包含两个单词之一的行:

wesbailey@feynman:~/tmp> grep -E 'apple|strudel' file.txt 
apple strudel
apple other txt
apple macintosh other text
other text strudel

查找包含这两个单词的行:

wesbailey@feynman:~/tmp> grep -E '(apple.*macintosh)|(macintosh.*apple)' file.txt 
apple macintosh other text

查找包含单词“apple”但不包含“macintosh”的行:

wesbailey@feynman:~/tmp> grep -E 'apple' file.txt | grep -v 'macintosh'
apple strudel
apple other txt

查找包含以下单词的行:确切的短语“一些单词”

grep '"some words"' file.txt

希望这有帮助

You can't do ratings with grep so those examples will require further logic. The other examples can be solved with or operator and the ?, *, and + qualifiers. Here a couple of examples of the regular expressions you need:

wesbailey@feynman:~/tmp> cat file.txt 
apple strudel
apple other txt
apple macintosh other text
macintosh other txt
other text strudel

Fnd rows that contain at least one of the two words:

wesbailey@feynman:~/tmp> grep -E 'apple|strudel' file.txt 
apple strudel
apple other txt
apple macintosh other text
other text strudel

Find rows that contain both words:

wesbailey@feynman:~/tmp> grep -E '(apple.*macintosh)|(macintosh.*apple)' file.txt 
apple macintosh other text

Find rows that contain the word “apple” but not “macintosh”:

wesbailey@feynman:~/tmp> grep -E 'apple' file.txt | grep -v 'macintosh'
apple strudel
apple other txt

Find rows that contain the exact phrase “some words”

grep '"some words"' file.txt

Hope this helps

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