Search code examples
functionvariablesawksubstringlowercase

AWK variables and functions syntax


I would like to remove "_" and ":" in the texts and then convert the texts into lowercase in AWK but couldn't get it working. Here's a simple data.txt file:

Count_Part:AA,1,2,3,4,5
Name_Date:BB,4,5,6,7,8

and my script process.awk:

BEGIN{
  FS=",";
}
{
        print("raw -->", $1);
        replaced=$gsub(/_|:/, "", $1);
        print("replacement -->", $replaced);
        lowered=$tolower($replaced);
        print("to lowercase -->", $lowered);
        print("\n");
}

but what I get from cat data.txt | awk -f process.awk is this, which is not what I expected:

raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> CountPartAA 1 2 3 4 5


raw --> Name_Date:BB
replacement --> 6
to lowercase -->

I am wondering 1) why the CountPartAA is not printed as countpartaa, and 2) why the 2nd row of the data.txt did not have the similar output as the 1st row did.

I suspect it's due to the variable assignment and function return syntax but I could not make it work. My expected output is like this:

raw --> Count_Part:AA
replacement --> CountPartAA
to lowercase --> countpartaa


raw --> Name_Date:BB
replacement --> NameDateBB
to lowercase --> namedatebb

Please kindly help. Thanks!


Solution

  • You are close, need to remove sigills $ on functions and variables apart $1 :

    BEGIN{
      FS=",";
    }
    {
            print("raw -->", $1);
            gsub(/_|:/, "", $1);     # return integer and modify $1 directly 
            replaced=$1
            print("replacement -->", replaced);
            lowered=tolower(replaced);
            print("to lowercase -->", lowered);
            print("\n");
    }
    

    output

    raw --> Count_Part:AA
    replacement --> CountPartAA
    to lowercase --> countpartaa
    
    
    raw --> Name_Date:BB
    replacement --> NameDateBB
    to lowercase --> namedateb