1e6d6c189SCody Peter Mello#!/bin/bash 2e6d6c189SCody Peter Mello 3e6d6c189SCody Peter Melloif [[ -z "$AWK" || -z "$WORKDIR" ]]; then 4e6d6c189SCody Peter Mello printf '$AWK and $WORKDIR must be set\n' >&2 5e6d6c189SCody Peter Mello exit 1 6e6d6c189SCody Peter Mellofi 7e6d6c189SCody Peter Mello 8e6d6c189SCody Peter MelloTEMP0=$WORKDIR/test.temp.0 9e6d6c189SCody Peter MelloTEMP1=$WORKDIR/test.temp.1 10e6d6c189SCody Peter MelloTEMP2=$WORKDIR/test.temp.2 11e6d6c189SCody Peter Mello 12e6d6c189SCody Peter MelloRESULT=0 13e6d6c189SCody Peter Mello 14e6d6c189SCody Peter Mellofail() { 15e6d6c189SCody Peter Mello echo "$1" >&2 16e6d6c189SCody Peter Mello RESULT=1 17e6d6c189SCody Peter Mello} 18e6d6c189SCody Peter Mello 19e6d6c189SCody Peter Melloecho T.split: misc tests of field splitting and split command 20e6d6c189SCody Peter Mello 21*3ee4fc2aSCody Peter Mello$AWK 'BEGIN { 22*3ee4fc2aSCody Peter Mello # Assign string to $0, then change FS. 23*3ee4fc2aSCody Peter Mello FS = ":"; 24*3ee4fc2aSCody Peter Mello $0="a:bc:def"; 25*3ee4fc2aSCody Peter Mello FS = "-"; 26*3ee4fc2aSCody Peter Mello print FS, $1, NF; 27*3ee4fc2aSCody Peter Mello 28*3ee4fc2aSCody Peter Mello # Assign number to $0, then change FS. 29*3ee4fc2aSCody Peter Mello FS = "2"; 30*3ee4fc2aSCody Peter Mello $0=1212121; 31*3ee4fc2aSCody Peter Mello FS="3"; 32*3ee4fc2aSCody Peter Mello print FS, $1, NF; 33*3ee4fc2aSCody Peter Mello}' > $TEMP1 34*3ee4fc2aSCody Peter Melloecho '- a 3 35*3ee4fc2aSCody Peter Mello3 1 4' > $TEMP2 36e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.1' 37e6d6c189SCody Peter Mello 38*3ee4fc2aSCody Peter Mello$AWK 'BEGIN { 39*3ee4fc2aSCody Peter Mello # FS changes after getline. 40*3ee4fc2aSCody Peter Mello FS = ":"; 41*3ee4fc2aSCody Peter Mello "echo a:bc:def" | getline; 42*3ee4fc2aSCody Peter Mello FS = "-"; 43*3ee4fc2aSCody Peter Mello print FS, $1, NF; 44*3ee4fc2aSCody Peter Mello}' > $TEMP1 45*3ee4fc2aSCody Peter Melloecho '- a 3' > $TEMP2 46e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.2' 47e6d6c189SCody Peter Mello 48e6d6c189SCody Peter Melloecho ' 49e6d6c189SCody Peter Melloa 50e6d6c189SCody Peter Melloa:b 51e6d6c189SCody Peter Melloc:d:e 52e6d6c189SCody Peter Melloe:f:g:h' > $TEMP0 53e6d6c189SCody Peter Mello$AWK 'BEGIN { 54e6d6c189SCody Peter Mello FS = ":" 55e6d6c189SCody Peter Mello while (getline <"'$TEMP0'" > 0) 56e6d6c189SCody Peter Mello print NF 57e6d6c189SCody Peter Mello}' > $TEMP1 58e6d6c189SCody Peter Melloecho '0 59e6d6c189SCody Peter Mello1 60e6d6c189SCody Peter Mello2 61e6d6c189SCody Peter Mello3 62e6d6c189SCody Peter Mello4' > $TEMP2 63e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.3' 64e6d6c189SCody Peter Mello 65*3ee4fc2aSCody Peter Mello# getline var shouldn't impact fields. 66*3ee4fc2aSCody Peter Mello 67*3ee4fc2aSCody Peter Melloecho 'f b a' > $TEMP0 68*3ee4fc2aSCody Peter Mello$AWK '{ 69*3ee4fc2aSCody Peter Mello FS = ":"; 70*3ee4fc2aSCody Peter Mello getline a < "/etc/passwd"; 71*3ee4fc2aSCody Peter Mello print $1; 72*3ee4fc2aSCody Peter Mello}' $TEMP0 > $TEMP1 73*3ee4fc2aSCody Peter Melloecho 'f' > $TEMP2 74*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.4' 75*3ee4fc2aSCody Peter Mello 76*3ee4fc2aSCody Peter Melloecho 'a b c d 77*3ee4fc2aSCody Peter Mellofoo 78*3ee4fc2aSCody Peter Melloe f g h i 79*3ee4fc2aSCody Peter Mellobar' > $TEMP0 80*3ee4fc2aSCody Peter Mello$AWK '{ 81*3ee4fc2aSCody Peter Mello FS=":"; 82*3ee4fc2aSCody Peter Mello getline v; 83*3ee4fc2aSCody Peter Mello print $2, NF; 84*3ee4fc2aSCody Peter Mello FS=" "; 85*3ee4fc2aSCody Peter Mello}' $TEMP0 > $TEMP1 86*3ee4fc2aSCody Peter Melloecho 'b 4 87*3ee4fc2aSCody Peter Mellof 5' > $TEMP2 88*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.5' 89*3ee4fc2aSCody Peter Mello 90*3ee4fc2aSCody Peter Melloecho 'a.b.c=d.e.f 91*3ee4fc2aSCody Peter Mellog.h.i=j.k.l 92*3ee4fc2aSCody Peter Mellom.n.o=p.q.r' > $TEMP0 93*3ee4fc2aSCody Peter Melloecho 'b 94*3ee4fc2aSCody Peter Melloh 95*3ee4fc2aSCody Peter Mellon' > $TEMP1 96*3ee4fc2aSCody Peter Mello$AWK 'BEGIN { FS="=" } { FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2 97*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 1)' 98*3ee4fc2aSCody Peter Mello 99*3ee4fc2aSCody Peter Melloecho 'a.b.c=d.e.f 100*3ee4fc2aSCody Peter Mellog.h.i=j.k.l 101*3ee4fc2aSCody Peter Mellom.n.o=p.q.r' > $TEMP0 102*3ee4fc2aSCody Peter Melloecho 'd.e.f 103*3ee4fc2aSCody Peter Mellob 104*3ee4fc2aSCody Peter Melloj.k.l 105*3ee4fc2aSCody Peter Melloh 106*3ee4fc2aSCody Peter Mellop.q.r 107*3ee4fc2aSCody Peter Mellon' > $TEMP1 108*3ee4fc2aSCody Peter Mello$AWK 'BEGIN { FS="=" } { print $2; FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2 109*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 2)' 110*3ee4fc2aSCody Peter Mello 111*3ee4fc2aSCody Peter Melloecho 'abc 112*3ee4fc2aSCody Peter Mellode 113*3ee4fc2aSCody Peter Mellof 114*3ee4fc2aSCody Peter Mello 115*3ee4fc2aSCody Peter Mello ' > $TEMP0 116*3ee4fc2aSCody Peter Mellowho | sed 10q >> $TEMP0 117*3ee4fc2aSCody Peter Mellosed 10q /etc/passwd >> $TEMP0 118*3ee4fc2aSCody Peter Mello 119*3ee4fc2aSCody Peter Mello$AWK ' 120*3ee4fc2aSCody Peter Mello{ n = split($0, x, "") 121*3ee4fc2aSCody Peter Mello m = length($0) 122*3ee4fc2aSCody Peter Mello if (m != n) print "error 1", NR 123*3ee4fc2aSCody Peter Mello s = "" 124*3ee4fc2aSCody Peter Mello for (i = 1; i <= m; i++) 125*3ee4fc2aSCody Peter Mello s = s x[i] 126*3ee4fc2aSCody Peter Mello if (s != $0) print "error 2", NR 127*3ee4fc2aSCody Peter Mello print s 128*3ee4fc2aSCody Peter Mello}' $TEMP0 > $TEMP1 129*3ee4fc2aSCody Peter Mello 130*3ee4fc2aSCody Peter Mellodiff $TEMP0 $TEMP1 || fail 'BAD: T.split 1' 131*3ee4fc2aSCody Peter Mello 132*3ee4fc2aSCody Peter Mello# assumes same test.temp.0! bad design 133*3ee4fc2aSCody Peter Mello 134*3ee4fc2aSCody Peter Mello 135*3ee4fc2aSCody Peter Mello$AWK ' 136*3ee4fc2aSCody Peter Mello{ n = split($0, x, //) 137*3ee4fc2aSCody Peter Mello m = length($0) 138*3ee4fc2aSCody Peter Mello if (m != n) print "error 1", NR 139*3ee4fc2aSCody Peter Mello s = "" 140*3ee4fc2aSCody Peter Mello for (i = 1; i <= m; i++) 141*3ee4fc2aSCody Peter Mello s = s x[i] 142*3ee4fc2aSCody Peter Mello if (s != $0) print "error 2", NR 143*3ee4fc2aSCody Peter Mello print s 144*3ee4fc2aSCody Peter Mello}' $TEMP0 > $TEMP1 145*3ee4fc2aSCody Peter Mello 146*3ee4fc2aSCody Peter Mellodiff $TEMP0 $TEMP1 || fail 'BAD: T.split //' 147*3ee4fc2aSCody Peter Mello 148*3ee4fc2aSCody Peter Mello$AWK ' 149*3ee4fc2aSCody Peter MelloBEGIN { FS = "" } 150*3ee4fc2aSCody Peter Mello{ n = split($0, x) # will be split with FS 151*3ee4fc2aSCody Peter Mello m = length($0) 152*3ee4fc2aSCody Peter Mello if (m != n) print "error 1", NR 153*3ee4fc2aSCody Peter Mello s = "" 154*3ee4fc2aSCody Peter Mello for (i = 1; i <= m; i++) 155*3ee4fc2aSCody Peter Mello s = s x[i] 156*3ee4fc2aSCody Peter Mello if (s != $0) print "error 2", NR 157*3ee4fc2aSCody Peter Mello print s 158*3ee4fc2aSCody Peter Mello}' $TEMP0 > $TEMP2 159*3ee4fc2aSCody Peter Mello 160*3ee4fc2aSCody Peter Mellodiff $TEMP0 $TEMP2 || fail 'BAD: T.split 2' 161*3ee4fc2aSCody Peter Mello 162*3ee4fc2aSCody Peter Mello# assumes same test.temp.0! 163*3ee4fc2aSCody Peter Mello 164*3ee4fc2aSCody Peter Mello$AWK ' 165*3ee4fc2aSCody Peter MelloBEGIN { FS = "" } 166*3ee4fc2aSCody Peter Mello{ n = NF 167*3ee4fc2aSCody Peter Mello m = length($0) 168*3ee4fc2aSCody Peter Mello if (m != n) print "error 1", NR 169*3ee4fc2aSCody Peter Mello s = "" 170*3ee4fc2aSCody Peter Mello for (i = 1; i <= m; i++) 171*3ee4fc2aSCody Peter Mello s = s $i 172*3ee4fc2aSCody Peter Mello if (s != $0) print "error 2", NR 173*3ee4fc2aSCody Peter Mello print s 174*3ee4fc2aSCody Peter Mello}' $TEMP0 > $TEMP2 175*3ee4fc2aSCody Peter Mello 176*3ee4fc2aSCody Peter Mellodiff $TEMP0 $TEMP2 || fail 'BAD: T.split 3' 177*3ee4fc2aSCody Peter Mello 178*3ee4fc2aSCody Peter Mello 179*3ee4fc2aSCody Peter Mello$AWK ' 180*3ee4fc2aSCody Peter Mello{ n = split( $0, temp, /^@@@ +/ ) 181*3ee4fc2aSCody Peter Mello print n 182*3ee4fc2aSCody Peter Mello}' > $TEMP1 <<XXX 183*3ee4fc2aSCody Peter Mello@@@ xxx 184*3ee4fc2aSCody Peter Mello@@@ xxx 185*3ee4fc2aSCody Peter Mello@@@ xxx 186*3ee4fc2aSCody Peter MelloXXX 187*3ee4fc2aSCody Peter Melloecho '2 188*3ee4fc2aSCody Peter Mello2 189*3ee4fc2aSCody Peter Mello2' > $TEMP2 190*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split 4' 191*3ee4fc2aSCody Peter Mello 192*3ee4fc2aSCody Peter Mellorm -f $WORKDIR/test.temp* 193*3ee4fc2aSCody Peter Mello 194*3ee4fc2aSCody Peter Melloecho ' 195*3ee4fc2aSCody Peter Melloa 196*3ee4fc2aSCody Peter Mellobc 197*3ee4fc2aSCody Peter Mellodef' > $TEMP0 198*3ee4fc2aSCody Peter Mello$AWK ' 199*3ee4fc2aSCody Peter Mello{ print split($0, x, "") 200*3ee4fc2aSCody Peter Mello}' $TEMP0 > $TEMP1 201*3ee4fc2aSCody Peter Melloecho '0 202*3ee4fc2aSCody Peter Mello1 203*3ee4fc2aSCody Peter Mello2 204*3ee4fc2aSCody Peter Mello3' > $TEMP2 205*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split null 3rd arg' 206*3ee4fc2aSCody Peter Mello 207*3ee4fc2aSCody Peter Mellorm -f $WORKDIR/test.temp* 208*3ee4fc2aSCody Peter Mello$AWK 'BEGIN { 209*3ee4fc2aSCody Peter Mello a[1]="a b" 210*3ee4fc2aSCody Peter Mello print split(a[1],a),a[1],a[2] 211*3ee4fc2aSCody Peter Mello}' > $TEMP1 212*3ee4fc2aSCody Peter Mello 213*3ee4fc2aSCody Peter Melloecho '2 a b' > $TEMP2 214*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split(a[1],a)' 215*3ee4fc2aSCody Peter Mello 216*3ee4fc2aSCody Peter Mello$AWK 'BEGIN { 217*3ee4fc2aSCody Peter Mello a = "cat\n\n\ndog"; 218*3ee4fc2aSCody Peter Mello split(a, b, "[\r\n]+"); 219*3ee4fc2aSCody Peter Mello print b[1], b[2]; 220*3ee4fc2aSCody Peter Mello}' > $TEMP1 221*3ee4fc2aSCody Peter Melloecho 'cat dog' > $TEMP2 222*3ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.split(a, b, "[\r\n]+")' 223*3ee4fc2aSCody Peter Mello 224*3ee4fc2aSCody Peter Mello 225e6d6c189SCody Peter Melloexit $RESULT 226