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:
- 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:
- git diff with line numbers and proper code alignment/indentation
- "BEGIN blocks must have an action part" error in awk script
- The whole point of me learning
awk
at all in the first place was to writegit diffn
. I just got it done: Git diff with line numbers (Git log with line numbers)