开发者

What does this awk scripts do?

开发者 https://www.devze.com 2023-03-11 08:28 出处:网络
I found this in a embedded in a Makefile: awk \'/@/{print \"\\\"\" $$_ \"\\\\n\\\"\" }\' file I know the prototype of awk is:

I found this in a embedded in a Makefile:

awk '/@/{print "  \"" $$_ "\\n\"" }' file

I know the prototype of awk is:

awk 'pattern {action}' file

But what does @ and $$_ mean?开发者_如何转开发


It looks like the underscore character is simply a variable in this case. And since it is not initialized, it has the value of zero. Thus, $_ is equivalent to $0, which refers to the entire line that was processed. I think that it could also have been written $x since x would be an uninitialized variable.

Since it appears in a makefile, two dollar signs are needed (it is a special character in a makefile) to produce a single dollar sign in the command.

And as already mentioned by Nemo, the @ is simply the pattern. Any line containing @ would be matched.


OK that is weird. It appears to have the same effect as:

awk '/@/{print "  \"" $_ "\\n\"" }' file

And also the same effect as:

awk '/@/{print "  \"" $0 "\\n\"" }' file

That is, it takes any line of the form foo@bar and converts it to "foo@bar\n" (with two leading spaces). Lines without an @ get dropped because they do not match the pattern.

But I have never seen the double-dollar sign, nor the use of $_ as a synonym, nor can I find them documented anywhere...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号