[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Read stdin delimited fails on empty field
From: |
Greg Wooledge |
Subject: |
Re: Read stdin delimited fails on empty field |
Date: |
Thu, 7 Jul 2016 16:51:59 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Thu, Jul 07, 2016 at 03:01:31PM -0400, Todd Merriman wrote:
> IFS=' ' # TAB
> If any fields are empty, the data is read into the preceding field.
> In other words, if in the example FLD3 is empty, FLD4 is read into
> FLD3. If FLD2 and FLD3 are empty, FLD4 is read into FLD1.
This is a feature. IFS treats "whitespace" characters (space, tab,
newline, maybe a few others) specially -- a group of consecutive
"IFS whitespace" characters is treated as a single delimiter, rather
than several adjacent delimiters.
If you want a "comma-separated value" treatment, you need to use a
delimiter which is not considered whitespace. E.g. you could pipe the
file through tr '\t' '\003' and then use IFS=$'\003' in the script.
Also note that it's better form to set IFS temporarily for the read
command, not globally for the entire script.
while IFS=$'\003' read -r fld1 fld2 fld3 fld4; do
...
done < <(tr '\t' '\003' < myfile)