For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2005 > IBM AIX AWK and Case Insensitivity









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author IBM AIX AWK and Case Insensitivity
Buck Turgidson

2005-02-02, 3:55 pm

I am running an awk on AIX, which unfortunately is not gawk. I am wondering
if there is a way to simplify these expressions, since I cannot use the
IGNORECASE=1 flag in this UNIX version of awk.



awk '{gsub(/width|WIDTH/,""); print}'



if ( ($0 ~ '/^[A-Z]/') || ($0 ~ '/^[a-z]/') ) {


Aharon Robbins

2005-02-02, 3:55 pm

In article <0hg7d2-oq4.ln1@turf.turgidson.com>,
Buck Turgidson <jc_va@hotmail.com> wrote:
>I am running an awk on AIX, which unfortunately is not gawk. I am wondering
>if there is a way to simplify these expressions, since I cannot use the
>IGNORECASE=1 flag in this UNIX version of awk.
>
>awk '{gsub(/width|WIDTH/,""); print}'


You may be able to use something like

$0 = tolower($0)

first, if you don't mind modifying your input line.

>if ( ($0 ~ '/^[A-Z]/') || ($0 ~ '/^[a-z]/') ) {


See if

$0 ~ /^[[:alpha:]]/

works under AIX.

HTH,
--
Aharon (Arnold) Robbins --- Pioneer Consulting Ltd. arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381 Fax: +1 206 350 8765
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
Ed Morton

2005-02-02, 3:55 pm



Buck Turgidson wrote:
> I am running an awk on AIX, which unfortunately is not gawk. I am wondering
> if there is a way to simplify these expressions, since I cannot use the
> IGNORECASE=1 flag in this UNIX version of awk.
>
>
>
> awk '{gsub(/width|WIDTH/,""); print}'


Maybe, but first a question: what would you want to do if the word
"Width" was in the input?

> if ( ($0 ~ '/^[A-Z]/') || ($0 ~ '/^[a-z]/') ) {


Get rid of those ticks, then either of these should work for you:

if ($0 ~ /^[A-Za-z]/)

or

if (tolower($0) ~ /^[a-z]/)

or

if ($0 ~ /^[[:alpha:]]/)

Regards,

Ed.
Ed Morton

2005-02-02, 8:56 pm



Ed Morton wrote:
>
>
> Buck Turgidson wrote:
>
>
>
> Maybe, but first a question: what would you want to do if the word
> "Width" was in the input?


I'm going to go ahead and assume your answer is that you'd want it to be
replaced too since you appear to want to set IGNORECASE. Given that, you
could define a function that figures out all cases of the letters in a
string, e.g.:

awk 'function ac(acIn) {
acOut = ""
c = split(acIn,acTmp,"")
for (i=1; i<=c; i++) {
acOut = acOut "[" tolower(acTmp[i]) toupper(acTmp[i]) "]"
}
return acOut
}
{ gsub(ac("width"),""); print }'

Regards,

Ed.
William James

2005-02-02, 8:56 pm


Buck Turgidson wrote:
> I am running an awk on AIX, which unfortunately is not gawk. I am

wondering
> if there is a way to simplify these expressions, since I cannot use

the
> IGNORECASE=1 flag in this UNIX version of awk.
>
>
>
> awk '{gsub(/width|WIDTH/,""); print}'
>
>
>
> if ( ($0 ~ '/^[A-Z]/') || ($0 ~ '/^[a-z]/') ) {


z BEGIN {
z s = "The Width is NOT correct."
z if ( match( tolower(s), /width/ ) )
z { _match( s )
z s = RLEFT "height" RRIGHT
z }
z if ( match( tolower(s), / not / ) )
z { _match( s )
z s = RLEFT " quite " RRIGHT
z }
z print s
z }
z
z # Sets RLEFT, RMATCH, and RRIGHT.
z function Match( s, r, which )
z { match( s, r )
z return _match( s, which )
z }
z function _match( s, which )
z { if ( RSTART )
z { RMATCH = substr(s, RSTART, RLENGTH )
z RLEFT = substr(s, 1, RSTART - 1)
z RRIGHT = substr(s, RSTART + RLENGTH )
z }
z else
z { RLEFT = s
z RMATCH = RRIGHT = ""
z }
z if ( !which || 2==which ) return RMATCH
z if (1==which) return RLEFT
z return RRIGHT
z }

The output is

The height is quite correct.

Buck Turgidson

2005-02-03, 3:55 pm

>
> awk 'function ac(acIn) {
> acOut = ""
> c = split(acIn,acTmp,"")
> for (i=1; i<=c; i++) {
> acOut = acOut "[" tolower(acTmp[i]) toupper(acTmp[i]) "]"
> }
> return acOut
> }
> { gsub(ac("width"),""); print }'
>
> Regards,
>
> Ed.



Thanks. BTW the "($0 ~ /^[[:alpha:]]/)" does work in AIX.

I think in a case like the above, I'll just drop back to good ol' sed.



William James

2005-02-03, 8:55 pm

Improved version. The function Match() can be told to ignore case.

Unlike the built-in match(), it sets RLEFT, RMATCH, and RRIGHT.

z BEGIN {
z s = "The Width of the part is NOT correct."
z if ( Match( s, "width", 1 ) )
z s = RLEFT "height" RRIGHT
z if ( Match( s, " not ", 1 ) )
z s = RLEFT " quite " RRIGHT
z if ( Match( s, "the " ) )
z s = RLEFT "this " RRIGHT
z print s
z }

The output is
The height of this part is quite correct.

z
z # Sets RLEFT, RMATCH, and RRIGHT.
z function Match( str, regexp, ignorecase, returnthis )
z { if (ignorecase)
z match( tolower(str), tolower(regexp) )
z else
z match( str, regexp )
z return _match( str, returnthis )
z }
z function _match( str, returnthis )
z { if ( RSTART )
z { RMATCH = substr(str, RSTART, RLENGTH )
z RLEFT = substr(str, 1, RSTART - 1)
z RRIGHT = substr(str, RSTART + RLENGTH )
z }
z else
z { RLEFT = str
z RMATCH = RRIGHT = ""
z }
z if (!returnthis) return RSTART
z if (1==returnthis) return RLEFT
z if (2==returnthis) return RMATCH
z return RRIGHT
z }

Ed Morton

2005-02-05, 3:55 pm



Buck Turgidson wrote:

<snip>
> Thanks. BTW the "($0 ~ /^[[:alpha:]]/)" does work in AIX.
>
> I think in a case like the above, I'll just drop back to good ol' sed.


You mean good new (gnu) sed if you want case-insensitive regex matching.
But if you have gnu sed you can have gnu awk and then you could use
IGNORECASE so we're back to square one.

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com