Re: Unix script question (basic)

From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 03/05/04


Date: Fri, 5 Mar 2004 22:10:20 +0100

2004-03-05, 18:44(+00), Piestiany7:
> Some time ago on these boards someone suggested a good
> way of finding out how many columns there were in a file
>
> read x < $1
> set -- $x
> columns=$#

That's not correct.

It should be:

set -f
set x `head -1 < "$1"`
shift
columns=$#

(otherwise, there's backslash processing and filename
generation).

or (less portable):

read -r x < "$1"
set -f
set -- $x
columns=$#

Best is probably:
columns=`awk '{exit}END{print NF}' < "$1"`

-- 
Stéphane                      ["Stephane.Chazelas" at "free.fr"]