gpt4 book ai didi

shell - grep 查找包含特定单词的行

转载 作者:行者123 更新时间:2023-12-02 00:39:25 25 4
gpt4 key购买 nike

我有一个文件 Batman.txt:

Batman lives in Gotham City.
In Gotham city batman is a vigilante
Gotham city has many criminals.
Batman and Joker live in Gotham city.
Superman and batman are very friendly, but batman is always prepared for
bad events unlike superman.
Superman lives in Metropolis city.
Batman has visited Metropolis city a couple of times.

我想将 bat 侠、哥谭和城市在同一行的行分开。即:

Batman lives in Gotham City.
In Gotham city batman is a vigilante
Batman and Joker live in Gotham city.

到目前为止我有这个:

grep -E -i "batman|gotham|city" batman.txt

但它没有提供必要的输出。我得到了其他不需要的行。

输出是:

Batman lives in Gotham City.
In Gotham city batman is a vigilante
Gotham city has many criminals.
Batman and Joker live in Gotham city.
Superman and batman are very friendly, but batman is always prepared for
Superman lives in Metropolis city.
Batman has visited Metropolis city a couple of times.

请帮忙。谢谢您

最佳答案

使用 grep -P 和前向正则表达式,因为您的关键字可以按任何顺序出现:

grep -iP '(?=.*?\bbatman\b)(?=.*?\bcity\b)(?=.*?\bgotham\b)' file
Batman lives in Gotham City.
In Gotham city batman is a vigilante
Batman and Joker live in Gotham city.

或者使用 awk:

awk '/[bB]atman/&&/[cC]ity/&&/[gG]otham/' file
Batman lives in Gotham City.
In Gotham city batman is a vigilante
Batman and Joker live in Gotham city.

关于shell - grep 查找包含特定单词的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000167/

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