xref: /aosp_15_r20/external/toybox/tests/awk.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/bash
2
3# Original found at http://lists.landley.net/pipermail/toybox-landley.net/2015-March/015201.html
4
5# Copyright 2015 Divya Kothari <[email protected]>
6
7# 2023: A few mods by Ray Gardner <[email protected]>
8#   See "awk -f test04.awk" near line 170
9#   and "awk -e ..." tests near line 415
10# 2024: Mods to use testcmd instead of testing for most tests
11#       Added new tests (after line 420)
12
13[ -f testing.sh ] && . testing.sh
14
15#testcmd "name" "command" "result" "infile" "stdin"
16#testing "name" "progname command" "result" "infile" "stdin"
17
18FILE1="abc def ghi 5\nghi jkl mno 10\nmno pqr stu 15\nstu vwx abc 20\n"
19FILE2="abc,def,ghi,5\nghi,jkl,mno,10\nmno,pqr,stu,15\nstu,vwx,abc,20\n"
20FILE3="abc:def:ghi:5\nghi:jkl:mno:10\nmno:pqr:stu:15\nstu:vwx:abc:20\n"
21FILE4="abc def ghi -5\nghi jkl mno -10\nmno pqr stu -15\nstu vwx abc -20\n"
22
23testcmd "awk PATTERN input" "'/abc/' input" \
24  "abc def ghi 5\nstu vwx abc 20\n" "$FILE1" ""
25
26testcmd "awk SUBPATTERN input" "'/ab/' input" \
27  "abc def ghi 5\nstu vwx abc 20\n" "$FILE1" ""
28
29testcmd "awk FIELD input" "'{print \$2,\$3}' input" \
30  "def ghi\njkl mno\npqr stu\nvwx abc\n" "$FILE1" ""
31
32testcmd "awk FIELD input (out range)" "'{print \$2,\$8}' input" \
33  "def \njkl \npqr \nvwx \n" "$FILE1" ""
34
35L1="def def def def def def def def def def"
36L2="jkl jkl jkl jkl jkl jkl jkl jkl jkl jkl"
37L3="pqr pqr pqr pqr pqr pqr pqr pqr pqr pqr"
38L4="vwx vwx vwx vwx vwx vwx vwx vwx vwx vwx"
39testing "awk FIELD input (single, multiple times)" \
40  "awk '{ print \$2,\$2,\$2,\$2,\$2,\$2,\$2,\$2,\$2,\$2 }' input" \
41  "$L1\n$L2\n$L3\n$L4\n" "$FILE1" ""
42
43
44HEAD="Head1\tHead2\tHead3"
45FOOT="Report Generated"
46testcmd "awk CODE input" "'BEGIN { print \"$HEAD\"; } {
47   print \$1,\"\t\",\$3; } END { print \"$FOOT\"; }' input" \
48  "$HEAD\nabc \t ghi\nghi \t mno\nmno \t stu\nstu \t abc\n$FOOT\n" "$FILE1" ""
49
50testcmd "awk '>' input" "'\$4>0' input" \
51  "abc def ghi 5\nghi jkl mno 10\nmno pqr stu 15\nstu vwx abc 20\n" "$FILE1" ""
52
53testcmd "awk '<' input" "'\$4<25' input" \
54  "abc def ghi 5\nghi jkl mno 10\nmno pqr stu 15\nstu vwx abc 20\n" "$FILE1" ""
55
56testcmd "awk '==' input" "'\$4==15' input" "mno pqr stu 15\n" "$FILE1" ""
57
58testcmd "awk CMP input" "'\$1~/abc/' input" "abc def ghi 5\n" "$FILE1" ""
59
60testcmd "awk COUNT input" "'BEGIN { count=0; } \$1~/abc/ { count++; } END {
61   print \"Total Count =\",count; }' input" "Total Count = 1\n" "$FILE1" ""
62
63testcmd "awk COLUMN input" "'{ print \$1 }' input" "abc\nghi\nmno\nstu\n" \
64  "$FILE1" ""
65
66testcmd "awk SUM input" "'BEGIN { sum=0; } { sum=sum+\$4; } END {
67   print \"Sum is =\",sum; }' input" "Sum is = 50\n" "$FILE1" ""
68
69testcmd "awk IF input" "'{ if(\$2 == \"jkl\") print \$1; }' input" "ghi\n" \
70  "$FILE1" ""
71
72testing "awk FOR MUL input" \
73  "awk 'BEGIN { for(i=1;i<=3;i++) print \"square of\", i, \"is\",i*i; }'" \
74  "square of 1 is 1\nsquare of 2 is 4\nsquare of 3 is 9\n" "" ""
75
76testing "awk FOR ADD input" \
77  "awk 'BEGIN { for(i=1;i<=3;i++) print \"twice of\", i, \"is\",i+i; }'" \
78  "twice of 1 is 2\ntwice of 2 is 4\ntwice of 3 is 6\n" "" ""
79
80testing "awk FOR SUB input" \
81  "awk 'BEGIN { for(i=1;i<=3;i++) print \"sub of\", i, \"is\",i-i; }'" \
82  "sub of 1 is 0\nsub of 2 is 0\nsub of 3 is 0\n" "" ""
83
84testcmd "awk {FS:invalid} input1" "'BEGIN { FS=\"69793793\" } { print \$2
85   }' input" "\n\n\n\n" "$FILE3" ""
86
87testcmd "awk -F invalid input1" "-F69793793 '{ print \$2 }' input" \
88  "\n\n\n\n" "$FILE3" ""
89
90testcmd "awk {FS} input2" "'BEGIN { FS=\",\" } { print \$2 }' input" \
91  "def\njkl\npqr\nvwx\n" "$FILE2" ""
92
93testcmd "awk -F input2" "-F\",\" '{ print \$2 }' input" \
94  "def\njkl\npqr\nvwx\n" "$FILE2" ""
95
96testcmd "awk {FS} input3" "'BEGIN { FS=\":\" } { print \$2 }' input" \
97  "def\njkl\npqr\nvwx\n" "$FILE3" ""
98
99testcmd "awk -F input3" "-F: '{ print \$2 }' input" "def\njkl\npqr\nvwx\n" \
100  "$FILE3" ""
101
102testcmd "awk {OFS} {1} input" "'BEGIN { OFS=\"__\" } { print \$2 }' input" \
103  "def\njkl\npqr\nvwx\n" "$FILE1" ""
104
105testcmd "awk {OFS} {1,2} input" "'BEGIN { OFS=\"__\" } { print \$2,\$3
106   }' input" "def__ghi\njkl__mno\npqr__stu\nvwx__abc\n" "$FILE1" ""
107
108testcmd "awk {NF} input" "'{print NF}' input" "4\n4\n4\n4\n" "$FILE1" ""
109
110testcmd "awk {NR} input" "'{print NR}' input" "1\n2\n3\n4\n" "$FILE1" ""
111
112testcmd "awk END{NR} input" "'END {print NR}' input" "4\n" "$FILE1" ""
113
114testcmd "awk SPLIT input" "'{ split(\$0,arr,\" \"); if(arr[3] == \"abc\")
115   print \$2 }' input" "vwx\n" "$FILE1" ""
116
117testcmd "awk SUBSTR input" "'{if (substr(\$0,1,3) == \"abc\") { print \$3 }
118   }' input" "ghi\n" "$FILE1" ""
119
120testcmd "awk SEARCH {PRINT} input" "'/ghi/ {print \$1,\$2,\$3,\$4}' input" \
121  "abc def ghi 5\nghi jkl mno 10\n" "$FILE1" ""
122
123testcmd "awk SEARCH {PRINTF} input" "'/ghi/ { printf \$1 \$2 \$3 \$4
124   }' input" "abcdefghi5ghijklmno10" "$FILE1" ""
125
126testcmd "awk {PRINT with TAB} input" "'{print \$2,\"\t\",\$4}' input" \
127  "def \t 5\njkl \t 10\npqr \t 15\nvwx \t 20\n" "$FILE1" ""
128
129testcmd "awk {PRINT 2,4} input" "'{print \$2,\$4}' input" \
130  "def 5\njkl 10\npqr 15\nvwx 20\n" "$FILE1" ""
131
132testcmd "awk {PRINT 4,2} input" "'{print \$4,\$2}' input" \
133  "5 def\n10 jkl\n15 pqr\n20 vwx\n" "$FILE1" ""
134
135testcmd "awk {PRINT X,Y} input" "'{print \$6,\$9}' input" \
136  " \n \n \n \n" "$FILE1" ""
137
138testcmd "awk {PRINT} input" "'{ print }' input" "$FILE1" "$FILE1" ""
139
140testcmd "awk INVALID_ARGS1 input" "'{ print x,y }' input" \
141  " \n \n \n \n" "$FILE1" ""
142
143testcmd "awk INVALID_ARGS2 input" "'{ print \$4,\$5 }' input" \
144  "5 \n10 \n15 \n20 \n" "$FILE1" ""
145
146testcmd "awk PATTERN input (not found)" "'/abcd/' input && echo 'yes'" \
147  "yes\n" "$FILE1" ""
148
149testcmd "awk {PATTERN:-ve} input" "'/-5/' input" "abc def ghi -5\n" \
150  "$FILE4" ""
151
152testcmd "awk FIELD input (high value)" "'{print \$99999}' input &&
153   echo 'yes'" "\n\n\n\nyes\n" "$FILE1" ""
154
155#### Starting "-f file" tests ####
156
157echo "{ if (\$1 == \"#START\") { FS=\":\"; } else if (\$1 == \"#STOP\") {
158  FS=\" \"; } else { print \$3 } }" > test.awk
159testcmd "awk -f test01.awk" "-f test.awk input" \
160  "ghi\nmno\nstu\nabc\n" "$FILE1" ""
161
162echo "BEGIN { i=1; while (i <= 5) { printf i \"-\" i*i \" \"; i=i+1; }
163  for (i=1; i <= 5; i++) { printf i \"-\" i*i \" \"; } }" > test.awk
164testcmd "awk -f test02.awk" "-f test.awk" \
165  "1-1 2-4 3-9 4-16 5-25 1-1 2-4 3-9 4-16 5-25 " "" ""
166
167echo "BEGIN { print \"Start.\" } { print \$1,\"-\",\$1*\$1; }
168  END { print \"End.\" }" > test.awk
169testcmd "awk -f test03.awk" "-f test.awk" \
170  "Start.\n5 - 25\n10 - 100\n15 - 225\n20 - 400\nEnd.\n" "" "5\n10\n15\n20\n"
171
172### echo "{ if ( \$0 ~ /:/ ) {FS=\":\";} else {FS=\" \";} print \$3 }" > test.awk
173### testing "awk -f test04.awk" "awk -f test.awk input" \
174###   "ghi\nmno\nstu\nabc\nghi\nmno\nstu\nabc\n" "$FILE1$FILE3" ""
175
176### TEST ERROR This test originally ended with:
177###   "ghi\nmno\nstu\nabc\nghi\nmno\nstu\nabc\n" "$FILE1$FILE3" ""
178### This is wrong; gawk/mawk/bbawk/bwk awk agree that second ghi should not appear.
179### (My current version of goawk agrees with the "wrong" expected value;
180###  I need to update to latest goawk and test again. rdg 2023-10-29)
181echo "{ if ( \$0 ~ /:/ ) {FS=\":\";} else {FS=\" \";} print \$3 }" > test.awk
182testcmd "awk -f test04.awk" "-f test.awk input" \
183  "ghi\nmno\nstu\nabc\n\nmno\nstu\nabc\n" "$FILE1$FILE3" ""
184
185echo "BEGIN { lines=0; total=0; } { lines++; total+=\$1; } END {
186  print \"Read:\",lines; print \"Total:\",total; if (lines > 0 ) {
187  print \"Average:\", total/lines; } else { print \"0\"; } }" > test.awk
188testcmd "awk -f test05.awk" "-f test.awk input" \
189  "Read: 5\nTotal: 150\nAverage: 30\n" "10\n20\n30\n40\n50\n" ""
190
191echo "BEGIN{FS=\":\";}{if(\$2==\"pqr\"){print \"first one:\", \$1;}}" > test.awk
192testcmd "awk -f test06.awk" "-f test.awk input" \
193  "first one: mno\n" "$FILE3" ""
194
195echo "{ print \$2; FS=\":\"; print \$2 }" > test.awk
196testcmd "awk -f test07.awk" "-f test.awk input" \
197  "\n\njkl\njkl\npqr\npqr\nvwx\nvwx\n" "$FILE3" ""
198
199echo "BEGIN { FS=\":\"; OFS=\":\"; } { \$2=\"\"; print }" > test.awk
200testcmd "awk -f test09.awk" "-f test.awk input" \
201  "abc::ghi:5\nghi::mno:10\nmno::stu:15\nstu::abc:20\n" "$FILE3" ""
202
203mkdir dir && touch dir/file && LLDATA="`ls -l dir`"
204rm -rf dir
205echo "{ if (NF==8) { print \$8; } else if (NF==9) { print \$9; } }" > test.awk
206testcmd "awk -f test10.awk" "-f test.awk input" "file\n" "$LLDATA" ""
207
208echo "{ if (NR >= 1) { print NR;} }" > test.awk
209testcmd "awk -f test11.awk" "-f test.awk input" "1\n2\n" "$LLDATA" ""
210
211echo "BEGIN { RS=\"\"; FS=\"\n\" } { print \$2,\$3; }" > test.awk
212testcmd "awk -f test12.awk" "-f test.awk input" \
213  "ghi jkl mno 10 mno pqr stu 15\n" "$FILE1" ""
214
215L="abc\ndef\nghi\n5\nghi\njkl\nmno\n10\nmno\npqr\nstu\n15\nstu\nvwx\nabc\n20\n"
216echo "BEGIN { RS=\" \"; } { print; }" > test.awk
217testcmd "awk -f test13.awk" "-f test.awk input" "$L\n" "$FILE1" ""
218
219L="abc def ghi 5\r\nghi jkl mno 10\r\nmno pqr stu 15\r\nstu vwx abc 20\r\n"
220echo "BEGIN { ORS=\"\r\n\" } { print }" > test.awk
221testcmd "awk -f test14.awk" "-f test.awk input" "$L" "$FILE1" ""
222
223echo "BEGIN { f=\"\"; }{ if(f != FILENAME){ f=FILENAME; print f }}" > test.awk
224testcmd "awk -f test15.awk" "-f test.awk input" "input\n" "$FILE1" ""
225
226echo "{ if (NF == 6) { } else { if (FILENAME == \"-\" ) { print \"ERROR\",
227  NR,\"line:\"; } else { print \"ERROR\",FILENAME,NR;}}}" > test.awk
228testcmd "awk -f test16.awk" "-f test.awk input" \
229  "ERROR input 1\nERROR input 2\nERROR input 3\nERROR input 4\n" "$FILE1" ""
230
231echo "BEGIN { number_of_users=0; } { if (NF>7) {
232  user=0; for (i=1; i<=number_of_users; i++) { if (username[i] == \$3) { user=i;
233  } } if (user == 0) { username[++number_of_users]=\$3; user=number_of_users; }
234  count[user]++; } } END { for (i=1; i<=number_of_users; i++) {
235  print count[i], username[i] } } " > test.awk
236testcmd "awk -f test17.awk" "-f test.awk input" "1 $USER\n" "$LLDATA" ""
237
238echo "{ usrname[\$3]++;}END{for(i in usrname){print usrname[i],i;} }" > test.awk
239testcmd "awk -f test18.awk" "-f test.awk input" "1 \n1 $USER\n" "$LLDATA" ""
240
241echo "{ if (NF>7) { username[\$3]++; } } END { for (i in username) {
242  print username[i], i; } }" > test.awk
243testcmd "awk -f test19.awk" "-f test.awk input" "1 $USER\n" "$LLDATA" ""
244
245echo "BEGIN { username[\"\"]=0; } { username[\$3]++; } END {
246  for (i in username) { if (i != \"\") { print username[i], i; }}}" > test.awk
247testcmd "awk -f test20.awk" "-f test.awk input" "1 $USER\n" "$LLDATA" ""
248
249echo "{ printf \"%5s %3d\n\", \$3, \$4; }" > test.awk
250testcmd "awk -f test22.awk" "-f test.awk input" \
251  "  ghi   5\n  mno  10\n  stu  15\n  abc  20\n" "$FILE1" ""
252
253echo "BEGIN { format1 =\"%8s %6sn\"; format2 =\"%8s %6dn\"; }
254  { printf(format2, \$1, \$4); }" > test.awk
255testcmd "awk -f test23.awk" "-f test.awk input" \
256  "     abc      5n     ghi     10n     mno     15n     stu     20n" "$FILE1" ""
257
258echo "END { for (i=1;i<=2;i++) {
259  printf(\"i=%d\n\", i) > \"ConcatedFile_a\" i; } }" > test.awk
260testcmd "awk -f test24.awk" "-f test.awk && cat ConcatedFile_a1 &&
261  cat ConcatedFile_a2 && rm -f ConcatedFile_a*" "i=1\ni=2\n" "" ""
262
263L1="             abc def ghi 5\n"
264L2="            ghi jkl mno 10\n"
265L3="            mno pqr stu 15\n"
266L4="            stu vwx abc 20\n"
267echo "{ if (length(\$0) < 80) { prefix = \"\";
268  for (i = 1;i<(40-length(\$0))/2;i++) { prefix = prefix \" \" };
269  print prefix \$0; } else { print; } }" > test.awk
270testcmd "awk -f test26.awk" "-f test.awk input" "$L1$L2$L3$L4" "$FILE1" ""
271
272echo "{ line = \$0; while (substr(line,length(line),1) == \"\\\\\") {
273  line = substr(line,1,length(line)-1); i=getline; if (i > 0) {
274  line = line \$0; } else { printf(\"%d\", NR); } } print line; }" > test.awk
275testcmd "awk -f test27.awk" "-f test.awk input" "$FILE1" "$FILE1" ""
276
277echo "BEGIN { for (x = 0; x <= 20; x++) { if (x == 5) { continue; }
278  printf \"%d \",x } print \"\" }" > test.awk
279testcmd "awk -f test28.awk" "-f test.awk" \
280  "0 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \n" "" ""
281
282echo "{ i = 1; while (i <= 2) { print \$i; i++ } }" > test.awk
283testcmd "awk -f test29.awk" "-f test.awk input" \
284  "abc\ndef\nghi\njkl\nmno\npqr\nstu\nvwx\n" "$FILE1" ""
285
286L1="abc def ghi 5\nabc def ghi 5\nabc def ghi 5\n"
287L2="ghi jkl mno 10\nghi jkl mno 10\nghi jkl mno 10\n"
288L3="mno pqr stu 15\nmno pqr stu 15\nmno pqr stu 15\n"
289L4="stu vwx abc 20\nstu vwx abc 20\nstu vwx abc 20\n"
290echo "{ i = 1; do { print \$0; i++ } while (i <= 3) }" > test.awk
291testcmd "awk -f test30.awk" "-f test.awk input" "$L1$L2$L3$L4" "$FILE1" ""
292
293echo "{ for (i = 1; i <= 3; i++) print \$i }" > test.awk
294testcmd "awk -f test31.awk" "-f test.awk input" \
295  "abc\ndef\nghi\nghi\njkl\nmno\nmno\npqr\nstu\nstu\nvwx\nabc\n" "$FILE1" ""
296
297echo "{ num = \$1; for (div = 2; div*div <= num; div++) { if (num % div == 0) {
298  break } } if (num % div == 0) { printf \"divisor of %d is %d\n\", num, div }
299  else { printf \"%d is prime\n\", num } }" > test.awk
300testcmd "awk -f test32.awk" "-f test.awk input" \
301  "divisor of 10 is 2\ndivisor of 15 is 3\n17 is prime\n" "10\n15\n17\n" ""
302
303# Mod in case prog name is not 'awk'
304##  echo "BEGIN { for (i = 0; i < ARGC; i++) { print ARGV[i] } }" > test.awk
305##  testcmd "awk -f test33.awk" "-f test.awk input1 input2 input3 input4" \
306##    "awk\ninput1\ninput2\ninput3\ninput4\n" "$FILE1" ""
307echo "BEGIN { for (i = 1; i < ARGC; i++) { print ARGV[i] } }" > test.awk
308testcmd "awk -f test33.awk" "-f test.awk input1 input2 input3 input4" \
309  "input1\ninput2\ninput3\ninput4\n" "$FILE1" ""
310
311echo "NR == 2 { NR = 17 } { print NR }" > test.awk
312testcmd "awk -f test34.awk" "-f test.awk input" "1\n17\n18\n19\n" \
313  "$FILE1" ""
314
315echo "BEGIN{n=0;}/abc/{++n;}END{print \"abc appears\",n,\"times\"}" > test.awk
316testcmd "awk -f test35.awk" "-f test.awk input" "abc appears 2 times\n" \
317  "$FILE1" ""
318
319echo "{ print \"Square root of\", \$1, \"is\", sqrt(\$1) }" > test.awk
320testcmd "awk -f test36.awk" "-f test.awk input" \
321  "Square root of 25 is 5\n" "25" ""
322
323FILE5="foo bar 2500\nabc def 2400\n"
324echo "\$1 == \"foo\" { print \$2 }" > test.awk
325testcmd "awk -f test37.awk" "-f test.awk input" "bar\n" "$FILE5" ""
326
327echo "/2400/ && /foo/" > test.awk
328testcmd "awk -f test38.awk" "-f test.awk input" "" "$FILE5" ""
329
330echo "/2400/ || /foo/" > test.awk
331testcmd "awk -f test39.awk" "-f test.awk input" "$FILE5" "$FILE5" ""
332
333echo "! /foo/" > test.awk
334testcmd "awk -f test40.awk" "-f test.awk input" "abc def 2400\n" "$FILE5" ""
335
336echo "\$1 ~ /foo/ { print \$2 }" > test.awk
337testcmd "awk -f test41.awk" "-f test.awk input" "bar\n" "$FILE5" ""
338
339echo "{ if (! (\$0 ~ /foo/)) print }" > test.awk
340testcmd "awk -f test42.awk" "-f test.awk input" "abc def 2400\n" "$FILE5" ""
341
342FILE6="Pat 100 97 58\nSandy 84 72 93\nChris 72 92 89\n"
343
344echo "{ print \"F1:\", \$1 }" > test.awk
345testcmd "awk -f test43.awk" "-f test.awk input" "F1: foo\nF1: abc\n" \
346  "$FILE5" ""
347
348echo "{ sum = \$2 + \$3 + \$4 ; avg = sum / 3; print \$1, avg }" > test.awk
349testcmd "awk -f test44.awk" "-f test.awk input" \
350  "Pat 85\nSandy 83\nChris 84.3333\n" "$FILE6" ""
351
352echo "{ print \$1 > \"list1\"; print \$2 > \"list2\" }" > test.awk
353testcmd "awk -f test45.awk" "-f test.awk input && cat list1 && cat list2" \
354  "Pat\nSandy\nChris\n100\n84\n72\n" "$FILE6" ""
355rm -f list1 list2
356
357echo "{ print \$(2*2) }" > test.awk
358testcmd "awk -f test46.awk" "-f test.awk input" "58\n93\n89\n" "$FILE6" ""
359
360echo "{ sub(/a+/,\"<A>\"); print }" > test.awk
361testcmd "awk -f test47.awk" "-f test.awk input" \
362  "P<A>t 100 97 58\nS<A>ndy 84 72 93\nChris 72 92 89\n" "$FILE6" ""
363
364echo "{ l[lines] = \$0; ++lines } END { for (i = lines-1; i >= 0; --i) {
365  print l[i]} }" > test.awk
366testcmd "awk -f test48.awk" "-f test.awk input" \
367  "Chris 72 92 89\nSandy 84 72 93\n\n" "$FILE6" ""
368
369FILE7="Pat 100 97 58 77 89 11 45\nSandy 84 729\nChris 92 89\nsagar 22 2213\n"
370L1="Pat 100 97 58 77 89 11 45"
371L2="sagar 22 2213"
372L3="dd 335566778856"
373testcmd "awk Print line longer than 12" "'length(\$0) > 12' input" \
374  "$L1\n$L2\n" "$FILE7" ""
375
376FILE8="red apple blue berry green thumb"
377testcmd "awk Print first two field opposite order" "'{ print \$2, \$1 }' input" \
378  "apple red\n" "$FILE8" ""
379
380FILE9="1, Justin Timberlake, Title 545, Price $7.30\n2, Taylor Swift, Title 723, Price $7.90\n3, Mick Jagger, Title 610, Price $7.90\n4, Lady Gaga, Title 118, Price $7.30\n5, Johnny Cash, Title 482, Price $6.50\n6, Elvis Presley, Title 335, Price $7.30\n7, John Lennon, Title 271, Price $7.90\n8, Michael Jackson, Title 373, Price $5.50\n"
381testcmd "awk filter data" "'{ print \$5 }' input" \
382  "545,\n723,\n610,\n118,\n482,\n335,\n271,\n373,\n" "$FILE9" ""
383
384FILE10="abcd efgh ijkl mnop\nqrst uvwx yzab cdef\nghij klmn opqr stuv\nwxyz abcd efgh ijkl\nmnop qrst uvwx yz\n"
385L1="abcd efgh ijkl mnop"
386L2="wxyz abcd efgh ijkl"
387testcmd "awk print selected lines" "'/abcd/' input" \
388   "$L1\n$L2\n" "$FILE10" ""
389L1="efgh mnop"
390L2="uvwx cdef"
391L3="klmn stuv"
392L4="abcd ijkl"
393L5="qrst yz"
394testcmd "awk print selected fields" "'{print \$2, \$4}' input" \
395   "$L1\n$L2\n$L3\n$L4\n$L5\n" "$FILE10" ""
396
397FILE11="abcd efgh ijkl mnop 4\nqrst uvwx yzab cdef 6\nghij klmn opqr stuv 0\nwxyz abcd efgh ijkl 1\nmnop qrst uvwx yz 2\n"
398FILE12="abcd\efgh\ijkl\mnop\4\nqrst\uvwx\yzab\cdef\6\nghij\klmn\opqr\stuv\0\nwxyz\abcd\efgh\ijkl\1\nmnop\qrst\uvwx\yz\2\n"
399testcmd "awk FS" "'BEGIN {FS=k;lksa;lkf;l} {print \$2}' input" "b\nr\nh\nx\nn\n" "$FILE11" ""
400
401echo "{ if (\$1 == \"#START\") { FS=\":\"; } else if (\$1 == \"#STOP\") {
402  FS=\" \"; } else { print \$3 } }" > test.awk
403testcmd "awk -v var=val -f test.awk" "-v var=2 -f test.awk input" \
404  "ghi\nmno\nstu\nabc\nghi\nmno\nstu\nabc\n" "$FILE1$FILE4" ""
405
406echo -e "abc def ghi 5\nghi jkl mno 10\nmno pqr stu 15\nstu vwx abc 20\n" > testfile1.txt
407echo -e "abc,def,ghi,5\nghi,jkl,mno,10\nmno,pqr,stu,15\nstu,vwx,abc,20\n" > testfile2.txt
408echo "{ if (\$1 == \"#START\") { FS=\":\"; } else if (\$1 == \"#STOP\") {
409  FS=\" \"; } else { print \$3 } }" > test.awk
410testcmd "awk -v myvar=val -f file1 file" "-v myvar=$2 -f test.awk testfile1.txt testfile2.txt" "ghi\nmno\nstu\nabc\n\n\n\n\n\n\n" "" ""
411
412### The -e option is non-standard. gawk and bbawk accept it; mawk and goawk do not, bwk awk says unknown option -e ignored but continues
413### bbawk does nothing useful with it: accepts -f and -e but runs the -e code out of order.
414### Correction: bbawk does do -e correctly now (since about December 2023?)
415
416###testing "awk -e print print ARGC file1 file2" "awk -e '{ print \$1; print ARGC }' testfile1.txt testfile2.txt" "abc\n3\nghi\n3\nmno\n3\nstu\n3\n\n3\nabc,def,ghi,5\n3\nghi,jkl,mno,10\n3\nmno,pqr,stu,15\n3\nstu,vwx,abc,20\n3\n\n3\n" "" ""
417###testing "awk -e print ARGC file" "awk -e '{ print ARGC }' testfile1.txt" "2\n2\n2\n2\n2\n" "$FILE1" ""
418###testing "awk -e print print ARGC input" "awk -e '{ print \$1; print ARGC }' input" "abc\n2\nghi\n2\nmno\n2\nstu\n2\n" "$FILE1" ""
419
420
421# 2024: New tests -- not in Divya Kothari's original ...
422
423# Assigning NF=0 caused trouble
424testcmd "assign NF=0" "'BEGIN { \$0 = \"a b\"; print NF, \"x\" \$0 \"y\"; NF = 0; print NF, \"x\" \$0 \"y\" }'" "2 xa by\n0 xy\n" "" ""
425
426# The following has never had a problem but is a good test anyway
427testcmd "split on empty string" "'BEGIN { n = split(\"abc\", a, \"\");print n, length(a)}'" "3 3\n" "" ""
428# The following must be run with ASAN=1 to cause failure with older versions
429testcmd "split on empty regex" "'BEGIN { n = split(\"abc\", a, //);print n, length(a)}'" "3 3\n" "" ""
430
431testcmd "srand() seeds unix time seconds" "'{dt = srand(srand()) - \$0; ok = dt == 0 || dt == 1; print ok}'" "1\n" "" "`date +%s`"
432testcmd "srand() default seed is 1" "'BEGIN{ print srand()}'" "1\n" "" ""
433
434# A file with empty lines can be treated as multiline records if RS="".
435FILEMULTILINE="abc defxy ghi\njkl mno\n\n\npqr stu\nvwxy abc\n"
436
437testcmd "multiline 1" "'BEGIN { RS=\"\"; FS=\"\"}; {print NR, NF, \$0; for (i=1;i<=NF;i++)printf \" %s %s\", i, \$i; print \"\"}'" "1 21 abc defxy ghi\njkl mno\n 1 a 2 b 3 c 4   5 d 6 e 7 f 8 x 9 y 10   11 g 12 h 13 i 14 \n 15 j 16 k 17 l 18   19 m 20 n 21 o\n2 16 pqr stu\nvwxy abc\n 1 p 2 q 3 r 4   5 s 6 t 7 u 8 \n 9 v 10 w 11 x 12 y 13   14 a 15 b 16 c\n" "" "$FILEMULTILINE"
438testcmd "multiline 2" "'BEGIN { RS=\"\"; FS=\" \"}; {print NR, NF, \$0; for (i=1;i<=NF;i++)printf \" %s %s\", i, \$i; print \"\"}'" "1 5 abc defxy ghi\njkl mno\n 1 abc 2 defxy 3 ghi 4 jkl 5 mno\n2 4 pqr stu\nvwxy abc\n 1 pqr 2 stu 3 vwxy 4 abc\n" "" "$FILEMULTILINE"
439testcmd "multiline 3" "'BEGIN { RS=\"\"; FS=\"x\"}; {print NR, NF, \$0; for (i=1;i<=NF;i++)printf \" %s %s\", i, \$i; print \"\"}'" "1 3 abc defxy ghi\njkl mno\n 1 abc def 2 y ghi 3 jkl mno\n2 3 pqr stu\nvwxy abc\n 1 pqr stu 2 vw 3 y abc\n" "" "$FILEMULTILINE"
440testcmd "multiline 4" "'BEGIN { RS=\"\"; FS=\"[ ]\"}; {print NR, NF, \$0; for (i=1;i<=NF;i++)printf \" %s %s\", i, \$i; print \"\"}'" "1 4 abc defxy ghi\njkl mno\n 1 abc 2 defxy 3 ghi\njkl 4 mno\n2 3 pqr stu\nvwxy abc\n 1 pqr 2 stu\nvwxy 3 abc\n" "" "$FILEMULTILINE"
441testcmd "multiline 5" "'BEGIN { RS=\"\"; FS=\"xy\"}; {print NR, NF, \$0; for (i=1;i<=NF;i++)printf \" %s %s\", i, \$i; print \"\"}'" "1 2 abc defxy ghi\njkl mno\n 1 abc def 2  ghi\njkl mno\n2 2 pqr stu\nvwxy abc\n 1 pqr stu\nvw 2  abc\n" "" "$FILEMULTILINE"
442
443# A "null" RS other than an empty string, e.g. "()" cannot match anywhere and most awks will take the entire file as one record.
444# A bug in earlier versions (also in busybox awk) cause infinite output on "null RS" test
445testcmd "null RS" "'BEGIN { RS=\"()\"; FS=\" \"}; {print NR, NF, \$0; for (i=1;i<=NF;i++)printf \" %s %s\", i, \$i; print \"\"}'" "1 9 abc defxy ghi\njkl mno\n\n\npqr stu\nvwxy abc\n\n 1 abc 2 defxy 3 ghi 4 jkl 5 mno 6 pqr 7 stu 8 vwxy 9 abc\n" "" "$FILEMULTILINE"
446
447testcmd "split() utf8"  "'BEGIN{n = split(\"aβc\", a, \"\"); printf \"%d %d\", n, length(a);for (e = 1; e <= n; e++) printf \" %s %s\", e, \"(\" a[e] \")\";print \"\"}'" "3 3 1 (a) 2 (β) 3 (c)\n" "" ""
448testcmd "split fields utf8"  "'BEGIN{FS=\"\"}; {printf \"%d\", NF; for (e = 1; e <= NF; e++) printf \" %s %s\", e, \"(\" \$e \")\"; print \"\"}'" "3 1 (a) 2 (β) 3 (c)\n" "" "aβc"
449
450testcmd "nextfile" " '{print NR, FNR, \$0};/ghi jkl/{nextfile}/ghi,jkl/{nextfile}' testfile1.txt testfile2.txt" "1 1 abc def ghi 5\n2 2 ghi jkl mno 10\n3 1 abc,def,ghi,5\n4 2 ghi,jkl,mno,10\n" "" ""
451
452testcmd "getline var numeric string bug fixed 20240514"  "'BEGIN{getline var; print (var < 10.0)}'" "1\n" "" "5.0\n"
453
454testcmd "lshift()" "'BEGIN{print lshift(3,2)}'" "12\n" "" ""
455testcmd "lshift() 64 bit" "'BEGIN{print lshift(1,40)}'" "1099511627776\n" "" ""
456testcmd "rshift()" "'BEGIN{print rshift(12, 1)}'" "6\n" "" ""
457testcmd "rshift() 64 bit" "'BEGIN{print rshift(1099511627776,39)}'" "2\n" "" ""
458testcmd "and()" "'BEGIN{print and(16, 25)}'" "16\n" "" ""
459testcmd "and(a, b, ...)" "'BEGIN{print and(16, 25, 10+16)}'" "16\n" "" ""
460testcmd "or()" "'BEGIN{print or(256, 16)}'" "272\n" "" ""
461testcmd "or(a, b, ...)" "'BEGIN{print or(256, 16, 8)}'" "280\n" "" ""
462testcmd "toupper()" "'BEGIN{print toupper(\"abABcD\")}'" "ABABCD\n" "" ""
463testcmd "tolower()" "'BEGIN{print tolower(\"abABcD\")}'" "ababcd\n" "" ""
464testcmd "substr()" "'BEGIN{print substr(\"abac\", 2, 2)}'" "ba\n" "" ""
465testcmd "atan2()" "'BEGIN{print substr(atan2(0, -1), 1, 5)}'" "3.141\n" "" ""
466testcmd "length()" "'{print length()}'" "1\n2\n0\n4\n" "" "a\n12\n\n6502"
467[ -n "$TEST_HOST" ] && export LC_ALL=en_US.UTF-8
468testcmd "length() utf8" "'{print length()}'< $FILES/utf8/japan.txt" "25\n" "" ""
469testcmd "substr() utf8" "'{print substr(\$0,2,1)}' < $FILES/utf8/arabic.txt" "ل\nأ\n" "" ""
470testcmd "index() utf8" "'{print index(\$0, \"ス\")}' < $FILES/utf8/japan.txt"\
471  "5\n" "" ""
472testcmd "tolower() utf8" "'{print tolower(\$0)}'" "ğжþ\n" "" "ĞЖÞ"
473testcmd "tolower() utf8 expand" "'{print tolower(\$0)}'" "ⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥⱥ\n"\
474  "" "ȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺȺ\n"
475testcmd "index() none" "'BEGIN{print index(\"ス\", \"deadbeef\")}'" "0\n" "" ""
476testcmd "index() same" "'BEGIN{print index(\"deadbeef\", \"deadbeef\")}'" "1\n" "" ""
477testcmd "match()" "'BEGIN{print match(\"bcdab\", \"ab\")}'" "4\n" "" ""
478testcmd "match() none" "'BEGIN{print match(\"ス\", \"deadbeef\")}'" "0\n" "" ""
479testcmd "match() utf8" "'{print match(\$0, \"ス\")}' < $FILES/utf8/japan.txt"\
480  "5\n" "" ""
481testcmd "\\u" "'BEGIN{print \"\\u20\u255\"}' < /dev/null" " ɕ\n" "" ""
482testcmd "printf %c" "'BEGIN{a=255; printf \"%c%c%c\", a, b, 255}'"\
483  "ÿ\0ÿ" "" ""
484
485testcmd "printf %c, 0" "'BEGIN{a=0; printf \"(%c)\", a}'" "(\000)" "" ""
486testcmd "printf %c, null_string" "'BEGIN{a=\"\"; printf \"(%c)\", a}'" "(\000)" "" ""
487testcmd "printf %c, utf8" "'BEGIN{a=\"ú\"; printf \"(%c)\", a}'" "(ú)" "" ""
488#testcmd "name" "command" "result" "infile" "stdin"
489
490testcmd "-b" "-b '{print length()}'< $FILES/utf8/japan.txt" "75\n" "" ""
491
492testcmd "awk -e print print ARGC file1 file2" "'{ print \$1; print ARGC }' testfile1.txt testfile2.txt" "abc\n3\nghi\n3\nmno\n3\nstu\n3\n\n3\nabc,def,ghi,5\n3\nghi,jkl,mno,10\n3\nmno,pqr,stu,15\n3\nstu,vwx,abc,20\n3\n\n3\n" "" ""
493testcmd "awk -e print ARGC file" "'{ print ARGC }' testfile1.txt" "2\n2\n2\n2\n2\n" "$FILE1" ""
494testcmd "awk -e print print ARGC input" "'{ print \$1; print ARGC }' input" "abc\n2\nghi\n2\nmno\n2\nstu\n2\n" "$FILE1" ""
495
496rm test.awk testfile1.txt testfile2.txt
497