Scrutinizer,
I think the OP is saying the input file has lines with 4 fields using <tab> as a separator and some fields are empty. Unfortunately, when IFS is set to <tab> (or <space>) groups of <tab>s (or <space>s and <tab>s) are treated as a single field separator. I don't believe that this behavior for IFS set to a single <tab> character conforms to the standards, but since this seems to be existing practice in both bash
and ksh
, this may be considered a bug in the standard.
benalt,
If I understand what you're trying to do, the following should work with ksh
(or any shell that performs the parameter expansions required by the POSIX standards):
#!/bin/ksh
while IFS='' read -r line
do printf 'input:"%s"\n' "$line"
d=${line##* }
line=${line% *}
c=${line##* }
line=${line% *}
b=${line##* }
line=${line% *}
a=${line##* }
printf 'a=%s,b=%s,c=%s,d=%s\n' "$a" "$b" "$c" "$d"
done < file.txt
(where the whitespace in % *
and in * }
is a single literal <tab> character in all of the above parameter expansions.
If file.txt
contains:
a b all fields present d
a field 2 empty d
b field 1 empty d
fields 1 & 2 empty d
fields 1, 2, & 4 empty
a b c d \
e f g h
the output produced is:
input:"a b all fields present d"
a=a,b=b,c=all fields present,d=d
input:"a field 2 empty d"
a=a,b=,c=field 2 empty,d=d
input:" b field 1 empty d"
a=,b=b,c=field 1 empty,d=d
input:" fields 1 & 2 empty d"
a=,b=,c=fields 1 & 2 empty,d=d
input:" fields 1, 2, & 4 empty "
a=,b=,c=fields 1, 2, & 4 empty,d=
input:"a b c d \"
a=a,b=b,c=c,d=d \
input:"e f g h"
a=e,b=f,c=g,d=h