Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Jan 2000 11:20:47 +0100 (CET)
From:      Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
To:        freebsd-questions@FreeBSD.ORG, "Sivaram Neelakantan" <sivaramn@wipsys.ge.com>
Subject:   awk questions (was: no subject)
Message-ID:  <200001201020.LAA67810@dorifer.heim3.tu-clausthal.de>
In-Reply-To: <8668po$tss$1@atlantis.rz.tu-clausthal.de>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <sivaramn@wipsys.ge.com> 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){
    ... <load array>
}
(ARGIND > 1){
    ... <lookup up array for values got from 2nd file>
}

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]) {
        ... <load array>
    }
    ARGV[1] = "/dev/null";	# Skip the first one.
}
{
    ... <lookup up array for values got from 2nd file>
}

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




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