2

「Google」が含まれるすべての行を見つけたいlsofので、次のことを試しました。

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

これにより、「google」という単語で始まる行を含む出力が正しく生成されます。

しかし、次にこれを試してみると、csvには何も含まれていません:

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

\s*でも、これはスペースがいくつあってもいいと思っていました。この動作には何か理由がありますか? ありがとうございました。

4

1 に答える 1

5

\s does mean spaces and \s* does mean zero-or-more spaces but not in awk.

awk uses a different (older) regex engine.

For awk you want [[:space:]]* to match zero-or-more spaces. (That's a character class class of [:space:] in a character list [].)

That being said if you just care about google being in the output then you just need /google/.

If you want an word-anchored google then you want /\<google\>/.

As Ed Morton points out GNU Awk version 4.0+ added support for the \s metacharacter as well.

于 2015-06-29T22:33:01.420 に答える