我有一个文件 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.
我希望将包含“蝙蝠侠”、“哥谭市”和“城市”的行分开。即:
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
。想象一下,如果你有10个关键字,那么你将需要10个管道grep
命令。如果你想避免前瞻awk
是正确的命令在这里使用。 - anubhavaawk '/[bB]atman/&&/[cC]ity/&&/[gG]otham/&&!/joker/' file
,在awk中,/
用作正则表达式的分隔符。 - anubhavagrep -iP '(?=.*?\bbatman\b)(?=.*?\bcity\b)(?=.*?\bgotham\b)(?!.*?\bjoker\b)' batman.txt
- anubhava