Awk find in columns with "if then" statement and print results

I have a file1.txt

file1.txt

F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true"
F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true"
U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true"
V-120009218","Pincher","1-1-1-","grey","yes","yes","1-0-","Worcester","MA","true"

Basicly I need to check "column 3 and column 7":
If F- found than Female.
If no F- found check M-, if found than Male
If neither M- or F- found than Unknown

Than print it like result1.txt.

result1.txt

F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true","Female"
F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true","Female"
U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true","Male"
V-120009218","Pincher","1-1-1-","grey","yes","yes","1-0-","Worcester","MA","true","Unknown"

Thanks.

Something like this should work:

awk '
    {
        if( match( "F-", $3 $7  ) )
            s = "Female";
        else
        if( match( "M-", $3 $7 ) )
            s = "Male";
        else
            s = "Unknown";
        printf( "%s,\"%s\"\n", $0, s );
    }
' input-file >output-file

1 Like

Wow that was quick. But it didn't work. They all came up with female.

Maybe the quotes are missing from the beginning of the lines:

"F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true"
"F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true"
"U-120009217","Lity","M-","grey","yes","yes","","Fall River","MA","true"
"V-120009218","Pincher","1-1-1-","grey","yes","yes","1-0-","Worcester","MA","true"

I tried to see if something was wrong by doing this:

if( match( "X-", $3 $7  ) s = "Female";

But again they all came up Female

Your code looks good; really can't figure it out.

Any ideas?

---------- Post updated at 11:27 PM ---------- Previous update was at 10:01 PM ----------

Your code was right on, just had to tweak it.

I think the following works, IF you can please give me confirmation that I recoded it correctly?

awk -F\",\" ' 
    {   
        if( match( $3 $7, "F-" ) )
            s = "Female";
        else
        if( match( $3 $7, "M-" ) )
            s = "Male";
        else
            s = "Unknown";
        printf( "%s,\"%s\"\n", $0, s );
    }
' input-file >output-file

And also is this the correct way to specify the field separator????

awk -F\",\"

Thanks!

Something similar...

awk -F, '
$3~"F-\""||$7~"F-\""{print $0OFS"\"Female\"";next}
$3~"M-\""||$7~"M-\""{print $0OFS"\"Male\"";next}
{print $0 OFS"\"Unknown\""} ' OFS=, infile

--ahamed

---------- Post updated at 10:55 PM ---------- Previous update was at 10:52 PM ----------

You don't need to escape the quotes in this case. Both of these are fine awk -F"," '...' or awk -F, '...'

--ahamed

1 Like

Other way:

$ awk 'BEGIN { FS = "," } $3 ~ /F-/ { print $0 ",\"Female\""; next } $3 ~ /M-/ { print $0 ",\"Male\""; next } { print $0 ",\"Unknown\"" }' infile
F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true","Female"
F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true","Female"
U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true","Male"
V-120009218","Pincher","1-1-1-","grey","yes","yes","1-0-","Worcester","MA","true","Unknown"

Regards,
Birei

1 Like