简体   繁体   English

在Mac终端中同时使用lsof和awk的奇怪行为?

[英]Strange behavior using lsof and awk together in Mac terminal?

I want to find all of the lines in lsof with "Google" in them, so I tried the following: 我想在lsof找到所有带有“ Google”的行,因此我尝试了以下操作:

lsof |  awk '/.*google.*/ { print $1 "," $2 "," $3} ' > new_file.csv

which yields correctly an output with rows starting with the word "google". 正确地产生输出以“ google”开头的行的输出。

But, then I try this and the csv contains nothing: 但是,然后我尝试这样做,csv不包含任何内容:

lsof |  awk '/\s*google.*/ { print $1 "," $2 "," $3} ' > new_file.csv

But, I thought that the \\s* means any number of spaces. 但是,我认为\\s*表示任意数量的空格。 Is there any reason for this behavior? 是否有任何这种行为的原因? Thank you. 谢谢。

\\s does mean spaces and \\s* does mean zero-or-more spaces but not in awk. \\s表示空格, \\s*表示零个或多个空格,但不是awk。

awk uses a different (older) regex engine. awk使用其他(较旧的)正则表达式引擎。

For awk you want [[:space:]]* to match zero-or-more spaces. 对于awk,您希望[[:space:]]*匹配零个或多个空格。 (That's a character class class of [:space:] in a character list [] .) (这是字符列表[][:space:]的字符类类。)

That being said if you just care about google being in the output then you just need /google/ . 话虽这么说,如果您只是关心google在输出中,那么您只需要/google/

If you want an word-anchored google then you want /\\<google\\>/ . 如果您想使用单词锚定的google则需要/\\<google\\>/

As Ed Morton points out GNU Awk version 4.0+ added support for the \\s metacharacter as well. 正如Ed Morton指出的那样,GNU Awk 4.0+版本还添加了对\\s元字符的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM