awk/testdir/T.split

225 lines
3.5 KiB
Plaintext
Raw Normal View History

2019-07-16 13:50:23 -04:00
#!/bin/sh
2019-06-23 05:13:57 -04:00
awk=${awk-../a.out}
2019-07-16 13:50:23 -04:00
WORKDIR=$(mktemp -d /tmp/nawktest.XXXXXX)
TEMP0=$WORKDIR/test.temp.0
TEMP1=$WORKDIR/test.temp.1
TEMP2=$WORKDIR/test.temp.2
RESULT=0
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
fail() {
echo "$1" >&2
RESULT=1
}
echo T.split: misc tests of field splitting and split command
$awk 'BEGIN {
# Assign string to $0, then change FS.
FS = ":"
$0="a:bc:def"
FS = "-"
print FS, $1, NF
# Assign number to $0, then change FS.
FS = "2"
$0=1212121
FS="3"
print FS, $1, NF
}' > $TEMP1
echo '- a 3
3 1 4' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.1'
$awk 'BEGIN {
# FS changes after getline.
FS = ":"
"echo a:bc:def" | getline
FS = "-"
print FS, $1, NF
}' > $TEMP1
echo '- a 3' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.2'
2019-06-23 05:13:57 -04:00
echo '
a
a:b
c:d:e
2019-07-16 13:50:23 -04:00
e:f:g:h' > $TEMP0
2019-06-23 05:13:57 -04:00
$awk 'BEGIN {
FS = ":"
2019-07-16 13:50:23 -04:00
while (getline <"'$TEMP0'" > 0)
2019-06-23 05:13:57 -04:00
print NF
2019-07-16 13:50:23 -04:00
}' > $TEMP1
2019-06-23 05:13:57 -04:00
echo '0
1
2
3
2019-07-16 13:50:23 -04:00
4' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.3'
# getline var shouldn't impact fields.
echo 'f b a' > $TEMP0
$awk '{
FS = ":"
getline a < "/etc/passwd"
print $1
}' $TEMP0 > $TEMP1
echo 'f' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.4'
echo 'a b c d
foo
e f g h i
bar' > $TEMP0
$awk '{
FS=":"
getline v
print $2, NF
FS=" "
}' $TEMP0 > $TEMP1
echo 'b 4
f 5' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.5'
echo 'a.b.c=d.e.f
g.h.i=j.k.l
m.n.o=p.q.r' > $TEMP0
echo 'b
h
n' > $TEMP1
$awk 'BEGIN { FS="=" } { FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 1)'
echo 'a.b.c=d.e.f
g.h.i=j.k.l
m.n.o=p.q.r' > $TEMP0
echo 'd.e.f
b
j.k.l
h
p.q.r
n' > $TEMP1
$awk 'BEGIN { FS="=" } { print $2; FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 2)'
2019-06-23 05:13:57 -04:00
echo 'abc
de
f
2019-07-16 13:50:23 -04:00
' > $TEMP0
who | sed 10q >> $TEMP0
sed 10q /etc/passwd >> $TEMP0
2019-06-23 05:13:57 -04:00
$awk '
{ n = split($0, x, "")
m = length($0)
if (m != n) print "error 1", NR
s = ""
for (i = 1; i <= m; i++)
s = s x[i]
if (s != $0) print "error 2", NR
print s
2019-07-16 13:50:23 -04:00
}' $TEMP0 > $TEMP1
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
diff $TEMP0 $TEMP1 || fail 'BAD: T.split 1'
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
# assumes same test.temp.0! bad design
2019-06-23 05:13:57 -04:00
$awk '
{ n = split($0, x, //)
m = length($0)
if (m != n) print "error 1", NR
s = ""
for (i = 1; i <= m; i++)
s = s x[i]
if (s != $0) print "error 2", NR
print s
2019-07-16 13:50:23 -04:00
}' $TEMP0 > $TEMP1
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
diff $TEMP0 $TEMP1 || fail 'BAD: T.split //'
2019-06-23 05:13:57 -04:00
$awk '
BEGIN { FS = "" }
{ n = split($0, x) # will be split with FS
m = length($0)
if (m != n) print "error 1", NR
s = ""
for (i = 1; i <= m; i++)
s = s x[i]
if (s != $0) print "error 2", NR
print s
2019-07-16 13:50:23 -04:00
}' $TEMP0 > $TEMP2
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
diff $TEMP0 $TEMP2 || fail 'BAD: T.split 2'
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
# assumes same test.temp.0!
2019-06-23 05:13:57 -04:00
$awk '
BEGIN { FS = "" }
{ n = NF
m = length($0)
if (m != n) print "error 1", NR
s = ""
for (i = 1; i <= m; i++)
s = s $i
if (s != $0) print "error 2", NR
print s
2019-07-16 13:50:23 -04:00
}' $TEMP0 > $TEMP2
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
diff $TEMP0 $TEMP2 || fail 'BAD: T.split 3'
2019-06-23 05:13:57 -04:00
$awk '
{ n = split( $0, temp, /^@@@ +/ )
print n
2019-07-16 13:50:23 -04:00
}' > $TEMP1 <<XXX
2019-06-23 05:13:57 -04:00
@@@ xxx
@@@ xxx
@@@ xxx
XXX
echo '2
2
2019-07-16 13:50:23 -04:00
2' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split 4'
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
rm -f $WORKDIR/test.temp*
2019-06-23 05:13:57 -04:00
echo '
a
bc
2019-07-16 13:50:23 -04:00
def' > $TEMP0
2019-06-23 05:13:57 -04:00
$awk '
{ print split($0, x, "")
2019-07-16 13:50:23 -04:00
}' $TEMP0 > $TEMP1
2019-06-23 05:13:57 -04:00
echo '0
1
2
2019-07-16 13:50:23 -04:00
3' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split null 3rd arg'
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
rm -f $WORKDIR/test.temp*
2019-06-23 05:13:57 -04:00
$awk 'BEGIN {
a[1]="a b"
print split(a[1],a),a[1],a[2]
2019-07-16 13:50:23 -04:00
}' > $TEMP1
echo '2 a b' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a[1],a)'
$awk 'BEGIN {
a = "cat\n\n\ndog"
split(a, b, "[\r\n]+")
print b[1], b[2]
}' > $TEMP1
echo 'cat dog' > $TEMP2
diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a, b, "[\r\n]+")'
2019-06-23 05:13:57 -04:00
2019-07-16 13:50:23 -04:00
exit $RESULT