0

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

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

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

4

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.

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...

于 2012-09-11T16:48:26.023 回答