Awk is printing the second line from file 1 because you have not supplied any input to the awk command. By default, with no input supplied, awk reads from stdin. The way that Ksh and bash function, anything reading from stdin within a while with redirection like this, will also read from the redirected input. So, the while loop reads the first line, and when awk is called it reads the rest, starting with line 2.
If you run this simple script, you will notice that the awk prints all lines except the first, and the while exits after running the awk only once because the end of stdin is reached:
#!/usr/bin/env ksh
while read x
do
echo "read line: $x"
echo "running awk"
awk '{print ">>> " $0;}'
done <t1.data
exit
You are seeing awk print only one line because you have added the test NR == 1
; this is making the behaviour a bit more difficult to understand.
If your goal is to read each line from file 1 (the student records) and use awk to do some processing on each, maybe you should just let awk parse the file directly rather than reading each line into a shell variable and attempting to work on it with awk. It is certainly more efficient to let awk do the file i/o in this case. Here's a sample:
awk -F \| '
{
printf( "student %s has student id %d and lives in %s\n", $2, $1, $NF );
}
' <t1.data
I made assumptions about your input data and am sorry if they aren't correct, but this should provide enough illustration to get going again.
And for the record, '[' is NOT a shell built-in. It is a binary, or somtimes a sym-link to the test command, and generally 'lives' in /usr/bin. The [[ expression ]]
construct in Kshell and bash is built-in. Both expression types have similar, yet very different, functionality and using test is not as efficient.