gpt4 book ai didi

Grep:行中第一个以 ?并以?

转载 作者:行者123 更新时间:2023-12-04 21:57:53 24 4
gpt4 key购买 nike

我正在尝试执行一个 grep 命令,该命令在文件中查找第一个单词以“as”开头且其第一个单词也以“ng”结尾的所有行

我将如何使用 grep 执行此操作?

最佳答案

这应该只是这样做:

$ grep '^as\w*ng\b' file

正则说明:
^    # Matches start of the line
as # Matches literal string as
\w # Matches characters in word class
* # Quantifies \w to match either zero or more
ng # Matches literal string ng
\b # Matches word boundary

可能错过了奇怪的角落案例。

如果您只想打印匹配的单词而不是整行,请使用 -o选项:
$ grep -o '^as\w*ng\b' file

阅读 man grep有关可用选项的所有信息。

关于Grep:行中第一个以 ?并以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com