home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!Germany.EU.net!math.fu-berlin.de!Sirius.dfn.de!darwin.sura.net!wupost!sdd.hp.com!think.com!news.bbn.com!news.bbn.com!news
- From: cjross@bbn.com (chris ross)
- Newsgroups: comp.unix.questions
- Subject: Re: awk problems
- Date: 30 Aug 1992 04:19:41 GMT
- Lines: 64
- Message-ID: <la0j2tINNrtj@news.bbn.com>
- References: <32213@adm.brl.mil>
- NNTP-Posting-Host: bbn.com
-
- CHUCK@bkmain.poly-east-london.ac.uk (Chuck Foster) writes:
-
- >Is there any way to convert a string to lowercase/uppercase in awk scripts?
-
- gawk has toupper() and tolower(). In nawk and awk you're forced to do
- something supremely inelegant. (And in awk, you can't even define
- separate functions.)
-
- BEGIN {
- lowers = "abcdefghijklmnopqrstuvwxyz";
- uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- }
-
- function toupper (s) {
- new_s = "";
- len = length(s);
- for ( i = 1; i <= len; i++ )
- {
- c = substr(s, i, 1);
- if ((idx = index(lowers, c)) != 0)
- new_s = new_s substr(uppers, idx, 1);
- else
- new_s = new_s c;
- }
- return(new_s);
- }
-
- function tolower (s) {
- new_s = "";
- len = length(s);
- for ( i = 1; i <= len; i++ )
- {
- c = substr(s, i, 1);
- if ((idx = index(uppers, c)) != 0)
- new_s = new_s substr(lowers, idx, 1);
- else
- new_s = new_s c;
- }
- return(new_s);
- }
-
-
- >I've tried x = strupr($1); but this just seems to return a string as is.
-
- Not surprising. Since strupr isn't defined as a function, awk reads
- this as two adjacent expressions and performs string concatenation.
- If strupr isn't set, this yields $1.
-
- >Also, is it possible to find the offset of a token in the whole string? The
- >file I am operating upon is in fixed format, and a user's full name occurs
- >after column 68.
-
- Try substr($0, 69). With two args, substr reads from the specified
- offset (1-based) to the end of the string.
-
- >Forgive my ignorance; I've only just begun to experiment with this
- >interesting command ... :-)
-
- When you're about to tear your hair out, learn perl. :-)
-
-
- chris ross <cjross@bbn.com> BBN Advanced Simulations (617) 873-3272
- WARNING: article may contain flammable material. Do not expose to open
- flames. In case of accidental ignition, douse keyboard with water.
-