This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: awk gsub problem


On Sep 18 11:21, Corinna Vinschen wrote:
> On Sep 17 22:30, Lee wrote:
> > On 9/16/10, Corinna Vinschen wrote:
> > > On Sep 15 18:30, Lee wrote:
> > >> I don't know if this is just a problem with the cygwin version of awk,
> > >> me misunderstanding something or what, but it looks like gsub isn't
> > >> working correctly in awk:
> > >> $ sh /tmp/test.awk
> > >> s= ::0::  should = ::S0::
> > >>
> > >> $ cat /tmp/test.awk
> > >> awk '
> > >> BEGIN {
> > >>   s="Serial0"
> > >>   gsub("[a-z]","",s)
> > >>   printf("s= ::%s::  should = ::S0::\n", s)
> > >>   exit
> > >> } '
> > >>
> > >> I also tried it with IGNORECASE=0 and with "awk --traditional" - same
> > >> results.
> > > Works fine for me:
> > 
> > Comment out the 'set LANG=" and gsub works fine:
> > $ echo $LANG
> > C.UTF-8
> > 
> > $ sh /tmp/test.awk
> > s= ::S0::  should = ::S0::
> > 
> > $ export LANG=en_US.UTF-8
> > 
> > $ sh /tmp/test.awk
> > s= ::0::  should = ::S0::
> > 
> > So awk gsub works for me again - thank you!
> > 
> > Just out of curiosity, why would setting LANG to en_US break
> > case-sensitivity in gsub?
> 
> I don't know either.  I just asked the upstream maintainer.  At least it
> isn't a Cygwin problem, since it also behaves the same on Linux.

I got reply from the upstream maintainer.  Case-sensitivity in gsub is
not broken, rather it's really a language dependent difference.

If LANG is "en_US" or "en_US.utf8", then the regular expression "[a-z]"
does *not* correspond anymore to the ASCII codes.  Rather it corresponds
to something like "[aAbBcCdD...zZ]", independent of the actual character
encoding ISO-8859-1 or UTF-8.

What you really want is this:

  BEGIN {
    s="Serial0"
    gsub("[[:lower:]]","",s)
    printf("s= ::%s::  should = ::S0::\n", s)
    exit
  }

The "[[:lower:]]" expression always catches all valid lowercase letters,
independent of the langauge, territory, and charset used.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]