同一行上有多个grep关键字?

时间:2021-04-07 10:07:29

I'm using the command grep 3 times on the same line like this

我在同一行上使用命令grep 3次

ls -1F ./ | grep / | grep -v 0_*.* | grep -v undesired_result

is there a way to combine them into one command instead of having it to pipe it 3 times?

有没有办法将它们组合成一个命令,而不是让它管道3次?

1 个解决方案

#1


0  

There's no way to do both a positive search (grep <something>) and a negative search (grep -v <something>) in one command line, but if your grep supports -E (alternatively, egrep), you could do ls -1F ./ | grep / | grep -E -v '0_*.*|undesired_result' to reduce the sub-process count by one. To go beyond that, you'd have to come up with a specific regular expression that matches either exactly what you want or everything you don't want.

在一个命令行中无法进行正面搜索(grep )和否定搜索(grep -v ),但如果你的grep支持-E(或者egrep),你可以做ls - 1F ./ | grep / | grep -E -v'0 _ *。* | unwantedired_result'将子进程数减少一个。要超越这个范围,你必须想出一个特定的正则表达式,它可以完全匹配你想要的或者你不想要的一切。

Actually, I guess that first sentence isn't entirely true if you have egrep, but building the proper regular expression that correctly includes both the positive and negative parts and covers all possible orderings of the parts might be more frustrating than it's worth...

实际上,我猜第一句话并不完全正确,如果你有egrep,但建立正确包含正面和负面部分的正确的正则表达式并涵盖部分的所有可能的排序可能比它的价值更令人沮丧......

#1


0  

There's no way to do both a positive search (grep <something>) and a negative search (grep -v <something>) in one command line, but if your grep supports -E (alternatively, egrep), you could do ls -1F ./ | grep / | grep -E -v '0_*.*|undesired_result' to reduce the sub-process count by one. To go beyond that, you'd have to come up with a specific regular expression that matches either exactly what you want or everything you don't want.

在一个命令行中无法进行正面搜索(grep )和否定搜索(grep -v ),但如果你的grep支持-E(或者egrep),你可以做ls - 1F ./ | grep / | grep -E -v'0 _ *。* | unwantedired_result'将子进程数减少一个。要超越这个范围,你必须想出一个特定的正则表达式,它可以完全匹配你想要的或者你不想要的一切。

Actually, I guess that first sentence isn't entirely true if you have egrep, but building the proper regular expression that correctly includes both the positive and negative parts and covers all possible orderings of the parts might be more frustrating than it's worth...

实际上,我猜第一句话并不完全正确,如果你有egrep,但建立正确包含正面和负面部分的正确的正则表达式并涵盖部分的所有可能的排序可能比它的价值更令人沮丧......