Dhanabalan
Dhanabalan

Reputation: 572

AWK script to Substring on tab delmited line


I am trying to do awk script to one of my project. I stuck at the below scenario,

Persons.txt (Tab delimited file)

A   M   DHANABALAN  INDIA ....
B   F   SUBHA   US  

I want to replace second tab M with Male and F with Female and keep all other tab value as such.

Expected output

A   Male    DHANABALAN  INDIA ....
B   Female  SUBHA   US  

I am trying as below

My code

{ 
    if( $2 == "M"){
        print $1 "Male" 
    }else{
        print $1 "Female"
    }
}

Don't know how to print the remaining tabs. Pleas assist me on this.

Upvotes: 1

Views: 42

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185560

$ awk '{ 
    if($2=="M"){
        $2="Male" 
    }
    else{
        $2="Female"
    }
    {print}
}' file

or

$ awk '{$2=="M"?$2="Male":$2="Female"}1' file

Output :

A Male DHANABALAN INDIA ....
B Female SUBHA US

Upvotes: 2

Related Questions