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
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
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,但建立正确包含正面和负面部分的正确的正则表达式并涵盖部分的所有可能的排序可能比它的价值更令人沮丧......