user3768495
user3768495

Reputation: 4657

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!

Upvotes: 1

Views: 121

Answers (3)

Daweo
Daweo

Reputation: 36680

As already noted, you should not prefix function calls using $ so do not:

replaced=$gsub(/_|:/, "", $1);

but rather

replaced=gsub(/_|:/, "", $1);

You are also misunderstanding how gsub works, from GNU AWK User's Guide

gsub(regexp, replacement [, target])

Search target for all of the longest, leftmost, nonoverlapping matching substrings it can find and replace them with replacement.(...)The gsub() function returns the number of substitutions made.(...)

Therefore you might disregard return value if you just need changes in string, e.g. if you need to remove digits from first column you might just do

awk '{gsub(/[0-9]/, "", $1);print}' file.txt

Upvotes: 0

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2875

you mean like this ?

{m,n,g}awk '$!NF=tolower($!(NF=NF))' FS='[_:]' OFS=
 
countpartaa,1,2,3,4,5
namedatebb,4,5,6,7,8

Upvotes: 2

Gilles Quénot
Gilles Quénot

Reputation: 185560

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

Upvotes: 3

Related Questions