gpt4 book ai didi

带通配符的 grep

转载 作者:行者123 更新时间:2023-12-01 09:31:32 24 4
gpt4 key购买 nike

我想对文件中的以下字符串进行 grep:

directory1
directory2
directory3

有没有办法用grep同时对所有3个进行grep?

例如:
cat file.txt | grep directory[1-3]

不幸的是,以上不起作用

最佳答案

如果这些是您需要搜索的唯一字符串,请使用 -F (固定字符串的grep):

grep -F "directory1
directory2
directory3" file.txt

如果您想使用更高级的正则表达式进行 grep,请使用 -E (使用扩展正则表达式):
grep -E 'directory[1-3]' file.txt

注意有些 grep s(如 GNU grep)不需要 -E为了这个例子工作。

最后,请注意您需要引用正则表达式。如果你不这样做,你的 shell 有可能首先根据路径名扩展来扩展正则表达式(例如,如果你在当前目录中有一个名为“directory1”的文件/目录, grep directory[1-3] 将被你的 shell 变成 grep directory1 )。

关于带通配符的 grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783890/

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