Can the regex matching pattern for awk be placed above the opening brace of the action line, or must it be on the same line?

笑着哭i 提交于 2020-05-28 05:46:25

问题


I'm studying awk pretty fiercely to write a git diffn implementation which will show line numbers for git diff, and I want confirmation on whether or not this Wikipedia page on awk is wrong [Update: I've now fixed this part of that Wikipedia page, but this is what it used to say]:

(pattern)
{
   print 3+2
   print foobar(3)
   print foobar(variable)
   print sin(3-2)
}

Output may be sent to a file:

(pattern)
{
   print "expression" > "file name"
}

or through a pipe:

(pattern)
{
   print "expression" | "command"
}

Notice (pattern) is above the opening brace. I'm pretty sure this is wrong but need to know for certain before editing the page. What I think that page should look like is this:

/regex_pattern/ {
    print 3+2
    print foobar(3)
    print foobar(variable)
    print sin(3-2)
}

Output may be sent to a file:

/regex_pattern/ {
    print "expression" > "file name"
}

or through a pipe:

/regex_pattern/ {
    print "expression" | "command"
}

Here's a test to "prove" it. I'm on Linux Ubuntu 18.04.

1. test_awk.sh

gawk \
'
BEGIN
{
    print "START OF AWK PROGRAM"
}
'

Test and error output:

$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
gawk: cmd. line:3: BEGIN blocks must have an action part

But with this:

2. test_awk.sh

gawk \
'
BEGIN {
    print "START OF AWK PROGRAM"
}
'

It works fine!:

$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
START OF AWK PROGRAM

Another example (fails to provide expected output):

3. test_awk.sh

gawk \
'
/hey/ 
{
    print $0
}
'

Erroneous output:

$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
hey1
hey1
hello
hey2
hey2

But like this:

4. test_awk.sh

gawk \
'
/hey/ {
    print $0
}
'

It works as expected:

$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
hey1
hey2

Updates: after solving this problem, I just added these sections below:

Learning material:

  1. In the process of working on this problem, I just spent several hours and created these examples: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/tree/master/awk. These examples, comments, and links would prove useful to anyone getting started learning awk/gawk.

Related:

  1. git diff with line numbers and proper code alignment/indentation
  2. "BEGIN blocks must have an action part" error in awk script
  3. The whole point of me learning awk at all in the first place was to write git diffn. I just got it done: Git diff with line numbers (Git log with line numbers)

回答1:


I agree with you that the Wikipedia page is wrong. It's right in the awk manual:

A pattern-action statement has the form

     pattern { action }

A missing { action } means print the line; a missing pattern always matches. Pattern-action statements are separated by newlines or semicolons.

...

Statements are terminated by semicolons, newlines or right braces.

This the man page for the default awk on my Mac. The same information is in the GNU awk manual, it's just buried a little deeper. And the POSIX specification of awk states

An awk program is composed of pairs of the form:

pattern { action }

Either the pattern or the action (including the enclosing brace characters) can be omitted.

A missing pattern shall match any record of input, and a missing action shall be equivalent to:

{ print }



回答2:


You can see in you examples that instead of semicolons at the end of statements you can separate them with new lines. When you have

/regex/
{ ...
}

it's equivalent to /regex/; {...} which is equal to /regex/{print $0} {...} as you tested the behavior.

Note that BEGIN and END are special markers and they need action statements explicitly since for BEGIN {print $0} is not possible as the default action. That's why the open curly brace should be on the same line. Perhaps due to convenience but it's all consistent.



来源:https://stackoverflow.com/questions/61979177/can-the-regex-matching-pattern-for-awk-be-placed-above-the-opening-brace-of-the

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!