xref: /freebsd/contrib/one-true-awk/testdir/T.split (revision 02e9120893770924227138ba49df1edb3896112a)
1#!/bin/sh
2
3awk=${awk-../a.out}
4
5WORKDIR=$(mktemp -d /tmp/nawktest.XXXXXX)
6
7TEMP0=$WORKDIR/test.temp.0
8TEMP1=$WORKDIR/test.temp.1
9TEMP2=$WORKDIR/test.temp.2
10
11RESULT=0
12
13fail() {
14	echo "$1" >&2
15	RESULT=1
16}
17
18echo T.split: misc tests of field splitting and split command
19
20$awk 'BEGIN {
21	# Assign string to $0, then change FS.
22	FS = ":"
23	$0="a:bc:def"
24	FS = "-"
25	print FS, $1, NF
26
27	# Assign number to $0, then change FS.
28	FS = "2"
29	$0=1212121
30	FS="3"
31	print FS, $1, NF
32}' > $TEMP1
33echo '- a 3
343 1 4' > $TEMP2
35diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.1'
36
37$awk 'BEGIN {
38	# FS changes after getline.
39	FS = ":"
40	"echo a:bc:def" | getline
41	FS = "-"
42	print FS, $1, NF
43}' > $TEMP1
44echo '- a 3' > $TEMP2
45diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.2'
46
47echo '
48a
49a:b
50c:d:e
51e:f:g:h' > $TEMP0
52$awk 'BEGIN {
53	FS = ":"
54	while (getline <"'$TEMP0'" > 0)
55		print NF
56}' > $TEMP1
57echo '0
581
592
603
614' > $TEMP2
62diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.3'
63
64# getline var shouldn't impact fields.
65
66echo 'f b a' > $TEMP0
67$awk '{
68	FS = ":"
69	getline a < "/etc/passwd"
70	print $1
71}' $TEMP0 > $TEMP1
72echo 'f' > $TEMP2
73diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.4'
74
75echo 'a b c d
76foo
77e f g h i
78bar' > $TEMP0
79$awk '{
80	FS=":"
81	getline v
82	print $2, NF
83	FS=" "
84}' $TEMP0 > $TEMP1
85echo 'b 4
86f 5' > $TEMP2
87diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.5'
88
89echo 'a.b.c=d.e.f
90g.h.i=j.k.l
91m.n.o=p.q.r' > $TEMP0
92echo 'b
93h
94n' > $TEMP1
95$awk 'BEGIN { FS="=" } { FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2
96diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 1)'
97
98echo 'a.b.c=d.e.f
99g.h.i=j.k.l
100m.n.o=p.q.r' > $TEMP0
101echo 'd.e.f
102b
103j.k.l
104h
105p.q.r
106n' > $TEMP1
107$awk 'BEGIN { FS="=" } { print $2; FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2
108diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 2)'
109
110echo 'abc
111de
112f
113
114     ' > $TEMP0
115who | sed 10q  >> $TEMP0
116sed 10q /etc/passwd >> $TEMP0
117
118$awk '
119{	n = split($0, x, "")
120	m = length($0)
121	if (m != n) print "error 1", NR
122	s = ""
123	for (i = 1; i <= m; i++)
124		s = s x[i]
125	if (s != $0) print "error 2", NR
126	print s
127}' $TEMP0 > $TEMP1
128
129diff $TEMP0 $TEMP1 || fail 'BAD: T.split 1'
130
131# assumes same test.temp.0!  bad design
132
133
134$awk '
135{	n = split($0, x, //)
136	m = length($0)
137	if (m != n) print "error 1", NR
138	s = ""
139	for (i = 1; i <= m; i++)
140		s = s x[i]
141	if (s != $0) print "error 2", NR
142	print s
143}' $TEMP0 > $TEMP1
144
145diff $TEMP0 $TEMP1 || fail 'BAD: T.split //'
146
147$awk '
148BEGIN { FS = "" }
149{	n = split($0, x)	# will be split with FS
150	m = length($0)
151	if (m != n) print "error 1", NR
152	s = ""
153	for (i = 1; i <= m; i++)
154		s = s x[i]
155	if (s != $0) print "error 2", NR
156	print s
157}' $TEMP0 > $TEMP2
158
159diff $TEMP0 $TEMP2 || fail 'BAD: T.split 2'
160
161# assumes same test.temp.0!
162
163$awk '
164BEGIN { FS = "" }
165{	n = NF
166	m = length($0)
167	if (m != n) print "error 1", NR
168	s = ""
169	for (i = 1; i <= m; i++)
170		s = s $i
171	if (s != $0) print "error 2", NR
172	print s
173}' $TEMP0 > $TEMP2
174
175diff $TEMP0 $TEMP2 || fail 'BAD: T.split 3'
176
177
178$awk '
179{ n = split( $0, temp, /^@@@ +/ )
180  print n
181}' > $TEMP1 <<XXX
182@@@ xxx
183@@@ xxx
184@@@ xxx
185XXX
186echo '2
1872
1882' > $TEMP2
189diff $TEMP1 $TEMP2 || fail 'BAD: T.split 4'
190
191rm -f $WORKDIR/test.temp*
192
193echo '
194a
195bc
196def' > $TEMP0
197$awk '
198{ print split($0, x, "")
199}' $TEMP0 > $TEMP1
200echo '0
2011
2022
2033' > $TEMP2
204diff $TEMP1 $TEMP2 || fail 'BAD: T.split null 3rd arg'
205
206rm -f $WORKDIR/test.temp*
207$awk 'BEGIN {
208  a[1]="a b"
209  print split(a[1],a),a[1],a[2]
210}' > $TEMP1
211
212echo '2 a b' > $TEMP2
213diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a[1],a)'
214
215$awk 'BEGIN {
216  a = "cat\n\n\ndog"
217  split(a, b, "[\r\n]+")
218  print b[1], b[2]
219}' > $TEMP1
220echo 'cat dog' > $TEMP2
221diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a, b, "[\r\n]+")'
222
223
224exit $RESULT
225