From owner-freebsd-questions Thu Jan 20 2:21: 6 2000 Delivered-To: freebsd-questions@freebsd.org Received: from dorifer.heim3.tu-clausthal.de (dorifer.heim3.tu-clausthal.de [139.174.243.252]) by hub.freebsd.org (Postfix) with ESMTP id 8D95F153A0 for ; Thu, 20 Jan 2000 02:21:02 -0800 (PST) (envelope-from olli@dorifer.heim3.tu-clausthal.de) Received: (from olli@localhost) by dorifer.heim3.tu-clausthal.de (8.9.3/8.9.3) id LAA67810; Thu, 20 Jan 2000 11:20:47 +0100 (CET) (envelope-from olli) Date: Thu, 20 Jan 2000 11:20:47 +0100 (CET) Message-Id: <200001201020.LAA67810@dorifer.heim3.tu-clausthal.de> From: Oliver Fromme To: freebsd-questions@FreeBSD.ORG, "Sivaram Neelakantan" Reply-To: freebsd-questions@FreeBSD.ORG Subject: awk questions (was: no subject) X-Newsgroups: list.freebsd-questions In-Reply-To: <8668po$tss$1@atlantis.rz.tu-clausthal.de> User-Agent: tin/1.4.1-19991201 ("Polish") (UNIX) (FreeBSD/3.4-19991219-STABLE (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Hi Sivaram, Please use a descriptive subject next time. Many people don't even look at mails which don't have a subject at all (because they're misdirected subscribe/unsubscribe commands most of the time). SIVARAM N wrote in list.freebsd-questions: > I hope this is the right list to ask... Well, actually your questions are not FreeBSD-specific. This kind of questions probably belong to some appropriate usenet newsgroup, e.g. comp.lang.awk. > 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? FS can be a regular expression, so you can construct it in a way so it matches the white space, too. For example: FS=", *" That one will match all spaces following a comma, so they will be parsed as part of the separator, thus they're removed. However, spaces at the very beginning of the line (before the first field) are still there. You can use the command sub(/^ */, "") to remove them. > 2. How can you find out whether the end of file has been reached for a specific file > given multiple files? There might be several ways to detect that. One of them is to look at the variable ARGIND, which contains the index of the currect file in the array ARGV. You could do something like this: (ARGIND == 1){ ... } (ARGIND > 1){ ... } Another way would be to read that file in your BEGIN statement and then set ARGV[1] to "/dev/null" (note that you can't change ARGIND inside the BEGIN block): BEGIN{ while(getline } ARGV[1] = "/dev/null"; # Skip the first one. } { ... } The second solutions is _slightly_ more efficient, because it doesn't have to check ARGIND for every input record. I'd recommend that you have a look at the awk manpage, it contains much of the details. Regards Oliver -- Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany (Info: finger userinfo:olli@dorifer.heim3.tu-clausthal.de) "In jedem Stück Kohle wartet ein Diamant auf seine Geburt" (Terry Pratchett) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message