-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathawk_syntax_tests.sh
More file actions
executable file
·208 lines (183 loc) · 5.77 KB
/
awk_syntax_tests.sh
File metadata and controls
executable file
·208 lines (183 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
# This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
# By: Gabriel Staples
# awk_syntax_tests.sh
# Started: 23 May 2020
# STATUS: IT WORKS! All examples in here are correct, have been tested, make valid points,
# and have correct comments.
# Run It:
# echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # 1. FAILS!
# # See reason why here:
# # https://stackoverflow.com/questions/27776583/begin-blocks-must-have-an-action-part-error-in-awk-script
# # $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # gawk: cmd. line:3: BEGIN blocks must have an action part
# gawk \
# '
# BEGIN
# {
# print "START OF AWK PROGRAM"
# }
# '
# ========= USE THIS! ========
# # 2. PASSES!
# # $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # START OF AWK PROGRAM
# gawk \
# '
# BEGIN {
# print "START OF AWK PROGRAM"
# }
# '
# # 3. FAILS TO PROVIDE EXPECTED OUTPUT!
# # Why? See this answer to my Q here, and my comments underneath it!
# # https://stackoverflow.com/questions/61979177/can-the-regex-matching-pattern-for-awk-be-placed-above-the-opening-brace-of-the/61979402#61979402
# # Essentially, `/hey/` acts as a stand-alone matcher with not action, so the default action of
# # printing $0 takes place, followed by a second rule which is to print, but has no matcher, so
# # the default of matching *everything* takes place! Hence, whatever matches /hey/ is printed, and
# # then *each line* is printed too! This results in duplication of the hey1 and hey2 prints!
# # Test results:
# # $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # hey1
# # hey1
# # hello
# # hey2
# # hey2
# gawk \
# '
# /hey/
# {
# print $0
# }
# '
# ========= USE THIS! ========
# # 4. WORKS AS EXPECTED!
# # $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # hey1
# # hey2
# gawk \
# '
# /hey/ {
# print $0
# }
# '
# ========= USE THIS! ========
# # 5. Also WORKS the same as 4! (is more explicit than 4.)
# # See, among other places, the example in the question here:
# # https://stackoverflow.com/questions/27776583/begin-blocks-must-have-an-action-part-error-in-awk-script
# gawk \
# '
# $0 ~ /hey/ {
# print $0
# }
# '
# # 6. Does NOT work! It's a syntax error because the `if()` statement needs an extra set of curly
# # braces around the *whole thing!*
# # $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # gawk: cmd. line:2: if ($0 ~ /hey/) {
# # gawk: cmd. line:2: ^ syntax error
# gawk \
# '
# if ($0 ~ /hey/) {
# print $0
# }
# '
# # 7. Also DOES work, the same as 4 and 5! (is more explicit still than 5. or 4.)
# # See this Q here for where I got the idea:
# # https://stackoverflow.com/questions/27776583/begin-blocks-must-have-an-action-part-error-in-awk-script
# # And here too: https://stackoverflow.com/questions/10739613/awk-if-else-issues
# # $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # hey1
# # hey2
# gawk \
# '
# {
# if ($0 ~ /hey/)
# {
# print $0
# }
# }
# '
# ========= USE THIS only *sometimes*!--when you really need an if/if-else type statement or
# really need to be extra explicit! ========
# # 8. Also DOES work, the same as 4 and 5 and 7! (is more explicit still than 5. or 4., and uses
# # more-consistent/"better" syntax than 7. because it keeps the opening brace `{` of the if statement
# # on the same line as the if statement, more-consistent with the pattern { action } syntax
# # *required* elsewhere!
# # $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# # hey1
# # hey2
# gawk \
# '
# {
# if ($0 ~ /hey/) {
# print $0
# }
# }
# '
# 9. This works too!
# Sample output:
# $ echo -e "hey1\nhello\nhey2" | ./awk_syntax_tests.sh
# START OF AWK PROGRAM
# hey1 Gabriel
# hey1 Gabriel #2
# hey2 Gabriel
# END OF AWK PROGRAM
gawk \
'
# ========= USE THIS! ========
BEGIN {
print "START OF AWK PROGRAM"
}
# Does NOT do what I expect it to! Rather, this assigns "Gabriel" to variable `name`, then it
# treats the result as a boolean expression, confirming in essence that name is in fact
# not zero/empty, so it considers it "true", and then runs the action for it,
# which is empty, so it uses the default action of `print $0`. Therefore, this one variable
# assignment line also prints the current record (line), which is not the intended behavior
# of the programmer!
# name = "Gabriel"
# To solve the above problem, either give it an empty action:
# (This works, but I prefer the next approach below more I think).
name = "Gabriel" {}
# ========= USE THIS! ========
# OR, make this variable assignment *part of* an action which gets applied to ALL records!
# (I prefer this approach to the above approach, and if you need to assign a bunch of variables
# at once, recommend you just do them all in a single rule like this at the beginning of your
# program, OR just do them individually wherever they are needed, as you see fit).
{
name = "Gabriel"
}
# ========= USE THIS! ========
/hey/ {
print $0, name
}
# # This, however, is NOT allowed!
# # "syntax error!"
# {
# /hey/ {
# # ^ syntax error
# print $0, name, " #2"
# }
# }
# # Neither is this!
# # "syntax error!"
# {
# $0 ~ /hey/ {
# # ^ syntax error
# print $0, name, " #2"
# }
# }
# ========= USE THIS! ========
# This, however, is just fine! Notice how the implicit "if"-less and tilde-less regexp check can
# ONLY go on the **OUTER-MOST SCOPE**! If you want to do a check on any scope (set of curly
# braces) inside of that, it MUST be done *explicitly* with `if` and `~` as shown just below!
/ey1/ {
if ($0 ~ /hey/) {
print $0, name" #2"
}
}
# ========= USE THIS! ========
END {
print "END OF AWK PROGRAM"
}
'