From owner-freebsd-questions Thu Jan 20 14:55:30 2000 Delivered-To: freebsd-questions@freebsd.org Received: from mx2.x-treme.gr (mx2.x-treme.gr [212.120.192.15]) by hub.freebsd.org (Postfix) with ESMTP id 1A40F15396 for ; Thu, 20 Jan 2000 14:55:21 -0800 (PST) (envelope-from keramida@diogenis.ceid.upatras.gr) Received: from localhost.hell.gr (pat8.x-treme.gr [212.120.197.200]) by mx2.x-treme.gr (8.9.3/8.9.3/IPNG-ADV-ANTISPAM-0.1) with ESMTP id AAA15140; Fri, 21 Jan 2000 00:55:15 +0200 Received: (from charon@localhost) by localhost.hell.gr (8.9.3/8.9.3) id SAA01090; Thu, 20 Jan 2000 18:30:20 +0200 (EET) (envelope-from keramida@diogenis.ceid.upatras.gr) Date: Thu, 20 Jan 2000 18:30:20 +0200 From: Giorgos Keramidas To: SIVARAM N Cc: freebsd-questions@FreeBSD.ORG Subject: 2 Awk questions (was: [no subject]) Message-ID: <20000120183020.D866@hades.hell.gr> Reply-To: keramida@ceid.upatras.gr References: <00e501bf6307$5cd13a80$8b2fa8c0@wipsys.ge.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0pre3i In-Reply-To: <00e501bf6307$5cd13a80$8b2fa8c0@wipsys.ge.com> X-PGP-Fingerprint: 62 45 D1 C9 26 F9 95 06 D6 21 2A C8 8C 16 C0 8E X-Phone-Number: +30-94-6203692, +30-93-2886457 X-Address: Theodorou Kirinaiou 61, 26334 Patra, Greece Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 = ; 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