1

I have an awk line :

awk -F'/|//'  '{for(i=1;i<=NF;i++) if($i=="CUST")break;print $(i)}'

I want the CUST to be case insensitive and I am using ($i==CUST) because the file contains words like CUSTOMER, which should not get matched.

I tried using Character class like: if($i=="[Cc][Uu][Ss][Tt]") but that throws an error.

fedorqui
  • 252,262
  • 96
  • 511
  • 570

2 Answers2

2

Your mistake is you are doing string comparison with == when the regular expression comparison operator is ~ and your regular expression string should be like /^[Cc][Uu][Ss][Tt]$/ (notice the anchors ^ and $ stop overmatching):

awk -F'/|//' '{for (i=1;i<=NF;i++) if ($i ~ /^[Cc][Uu[Ss][tT]$/)break; print $i}'

Better approachs would be to use the IGNORECASE variable or the tolower, toupper functions.

Chris Seymour
  • 79,902
  • 29
  • 153
  • 193
  • Will not this match both `CUST` and `CUSTOMER`? If I am correct OP does not like to match on `CUSTOMER` – Jotne Mar 04 '14 at 10:08
  • 1
    @Jotne right but `IGNORECASE` or one of the string functions is a better approach, was just pointing out the actual error in the OPs approach. – Chris Seymour Mar 04 '14 at 10:13
2

Use awk

toupper($i)

or

tolower($i)

Like this:

awk -F'/|//'  '{for (i=1;i<=NF;i++) if (tolower($i) == "cust")break; print $i}'
Jotne
  • 39,326
  • 11
  • 49
  • 54