shell脚本grep命令-c(count) - 命令行排序是否重要?

发布于 2025-01-25 20:31:55 字数 183 浏览 2 评论 0原文

只是一个关于疑问的快速问题。这两个区别有什么区别:

grep "$genre" "$i" | grep "$type" -c

他们也许

grep "$genre" "$i" | grep -c "$type"

做同样的事情?

just a quick question regarding a doubt. What's the difference between these two:

grep "$genre" "$i" | grep "$type" -c

and

grep "$genre" "$i" | grep -c "$type"

Do they do perhaps the same thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2025-02-01 20:31:55

grep“ $ type” -c的POSIX标准标准行为,假设$ type不会扩展到以仪表率开头的字符串,是对待-c作为文件名。

只有grep的非标准版本(例如GNU项目构建的版本以及在Linux系统上通常找到的版本)将把-c视为启用“计数”行为的选项不是在第一个位置论点之前。

编写您的脚本以需要非标准工具是不好的做法,除非它们从这些工具中获得一定的具体收益。使用GREP -C“ $ TYPE”

The POSIX standard-mandated behavior for grep "$type" -c, presuming that $type does not expand to a string starting with a dash, is to treat -c as a filename.

Only nonstandard versions of grep (such as the one built by the GNU project and commonly found on Linux systems) will treat -c as an option enabling "count" behavior when it isn't before the first positional argument.

It's bad practice to write your scripts to require nonstandard tools unless they gain some concrete benefit from those tools. Use grep -c "$type".

留蓝 2025-02-01 20:31:55

如您从man grep中看到的,-c交换机属于所谓的“常规输出控制”。这些开关可以放在grep ...命令中的不同位置,这对一般结果没有影响,因此,实际上,您提到的这两条线都是相等的。

As you can see from man grep, the -c switch belongs to the so-called "General Output Control". Those switches can be placed on different places in the grep ... command and this has no impact on the general outcome, so indeed, both lines you mention are equal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文