Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Jan 2000 18:30:20 +0200
From:      Giorgos Keramidas <charon@hades.hell.gr>
To:        SIVARAM N <sivaramn@wipsys.ge.com>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   2 Awk questions (was: [no subject])
Message-ID:  <20000120183020.D866@hades.hell.gr>
In-Reply-To: <00e501bf6307$5cd13a80$8b2fa8c0@wipsys.ge.com>
References:  <00e501bf6307$5cd13a80$8b2fa8c0@wipsys.ge.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jan 20, 2000 at 10:59:45AM +0530, SIVARAM N wrote:
> Hi,
> I hope this is the right list to ask...
> 
> I have 2 questions actually.
> 
> 1. I have a comma separated records in a file where most fields have
>    leading whitespaces. Setting FS="," & reading the file preserves
>    the leading whitespaces. How do I remove them?

You can remove leading whitespace with sed(1) or perl(1).  Then pipe
the input to awk(1), and you're ready to go, i.e. use something like:

	sed -e 's/^[ ]*//' < datafile |\
	awk '{ ... }'

> 2. How can you find out whether the end of file has been reached for
>    a specific file given multiple files?

I don't want to start anything like a flame here, but since you do not
seem to be very acquainted with awk(1), and I know that Perl does this
as easily as:

	# open the file for reading.
	$datafile = "/path/to/datafile";
	open(HANDLE, "< $datafile") || die "read error opening $datafile";

	# read the entire file in an array.
	@array = <HANDLE>;

Then with $array[0] you get the first line, $array[1] the second, and
so on... you get the point.  Oh, and the nice thing about Perl is that
you can remove leading whitespace without filtering the input from sed. 
This can be done even when processing the lines themselves, as in:

	$k = 0;
	while ($k <= $#array) {
	    $array[$k] =~ s/^[\s]*//;
	    # do something on $array[$k] ...
	    $k++;
	}

It is little details like this one that have made me prefer perl for
data manipulation, but I don't want to push my opinion on anyone else. 
If you'd prefer to stick with awk, then you can ignore this post and
live happily ever after ;)

Ciao.

-- 
Giorgos Keramidas, < keramida @ ceid . upatras . gr >
"Don't let your schooling interfere with your education." [Mark Twain]


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000120183020.D866>