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:
(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" }
这是“证明”它的测试。我在Linux Ubuntu 18.04上。
test_awk.sh
gawk \
'
BEGIN
{
print "START OF AWK PROGRAM"
}
'
测试和错误输出:
$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
gawk: cmd. line:3: BEGIN blocks must have an action part
但是有了这个:
test_awk.sh
gawk \
'
BEGIN {
print "START OF AWK PROGRAM"
}
'
工作正常!:
$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
START OF AWK PROGRAM
另一个示例(未能提供预期的输出):
test_awk.sh
gawk \
'
/hey/
{
print $0
}
'
错误输出:
$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
hey1
hey1
hello
hey2
hey2
但是像这样:
gawk \
'
/hey/ {
print $0
}
'
它按预期工作:
$ echo -e "hey1\nhello\nhey2" | ./test_awk.sh
hey1
hey2