[kwlug-disc] bash question tab separated values

Khalid Baheyeldin kb at 2bits.com
Thu Nov 5 16:32:06 EST 2009

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
  read LINE
  if [ "$?" != 0 ]; then  # It is an End Of File condition

  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
exec <&3 3<&-
Khalid M. Baheyeldin
2bits.com, Inc.
Drupal optimization, development, customization and consulting.
Simplicity is prerequisite for reliability. --  Edsger W.Dijkstra
Simplicity is the ultimate sophistication. --   Leonardo da Vinci
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://kwlug.org/pipermail/kwlug-disc_kwlug.org/attachments/20091105/89c9622e/attachment.html>

More information about the kwlug-disc mailing list