[ale] Thanks - and another Bash script question

Danny Cox danscox at mindspring.com
Fri May 16 11:18:14 EDT 2003


Bob,

On Thu, 2003-05-15 at 16:59, Bob Kruger wrote:
> I had the same problem here.
> 
> I have worked a way to do it that is a bit "clunky" and slow:
> 
> #check to see if the file is there.  If so, get a count of the #number of lines in the file
> if [ -f $1 ]  ; then
>    lc=$(wc -l < $1)
> fi
> ln=1
> while [ $ln -le $lc ] ; do
>    fl[$ln]=$(sed -n -e "$ln"p $1)
>    ls[0]=$(echo ${fl[$ln]} | cut -f1 --delimiter="," )
>    ls[1]=$(echo ${fl[$ln]} | cut -f2 --delimiter="," )

	Try something like this: use awk to seperate the fields, but print them
out as parameters to a function:

		printf "%c%s%c ", 047, $fieldn, 047

and use the $() around the whole thing to suck it in.  It may require an
eval, too.

	process () {
		# do something with $1, $2, etc.
	}
	eval $(awk '-F,' '
	{
		printf "process "
		for (fieldn = 1; fieldn <= NF; ++fieldn)
			printf "%c%s%c ", 047, $fieldn, 047
		print
	}' file)

	Note: the 047 is the octal value of a single quote (').  You have to do
it that way 'cuz there's no way to escape a single quote within a single
quoted string.

	I tried setting the array ls[0,1,2,3,...] to the args, but it didn't
parse that correctly.  The above seems to work, though.

	Good luck!

-- 
kernel, n.: A part of an operating system that preserves the
medieval traditions of sorcery and black art.

Danny

_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list