[kwlug-disc] bash question tab separated values
Robert P. J. Day
rpjday at crashcourse.ca
Thu Nov 5 23:37:12 EST 2009
On Thu, 5 Nov 2009, Khalid Baheyeldin wrote:
>
> On Thu, Nov 5, 2009 at 2:46 PM, Richard Weait <richard at weait.com> wrote:
> I'm reading lines from a tab-separated-value text file. Ten values
> per line. I'd like to load them into ten variables for further
> processing, then output.
>
> It ain't working for me. Looks like the tabs are being silently
> dropped then my cut -f3, for example, returns the complete line.
>
> Some fields include spaces, so I'd rather continue to split on
> tabs.
> How do I keep the tabs? How should I be reading these lines?
>
>
> Can you process in awk?
>
> If so, then just use:
>
> awk -F"\t" '{print $1; ... something else; }' yourfile.txt
>
> If you still insist on doing it in shell, then do this:
>
> exec 3>&1 < file.txt
> while true
> do
> read LINE
> if [ "$?" != 0 ]; then # It is an End Of File condition
> break
> fi
>
> FIELD0=`echo "$LINE" | awk -F"\t" '{print $1}'`
> FIELD1=`echo "$LINE" | awk -F"\t" '{print $2}'`
> FIELD2=`echo "$LINE" | awk -F"\t" '{print $2}'`
>
> echo $FIELD0
> echo $FIELD2
> done
> exec <&3 3<&-
can't you just set the internal field separator before reading in
the file in a bash loop:
IFS="<TAB>"
while read v1 v2 v3 ...etc... v10 ; do
... whatever with v1 v2 v3 ...
done < inputfile
the proper way to set the IFS would be (from experience), typing the
first quote, then a ^v, followed by pressing the TAB key, then
finishing off with the closing quote. the ^v means "take the next
character absolutely literally."
i just tested that and it seems to work.
rday
--
========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA
Linux Consulting, Training and Kernel Pedantry.
Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
More information about the kwlug-disc
mailing list