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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk find in columns with "if then" statement and print results
# 1  
Old 01-08-2012
Awk find in columns with "if then" statement and print results

I have a file1.txt

file1.txt
Code:
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
Code:
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.
# 2  
Old 01-08-2012
Something like this should work:


Code:
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

This User Gave Thanks to agama For This Post:
# 3  
Old 01-09-2012
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:

Code:
"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:

Code:
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?
Code:
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????

Code:
awk -F\",\"

Thanks!
# 4  
Old 01-09-2012
Something similar...
Code:
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 ----------

Quote:
Originally Posted by charles33
And also is this the correct way to specify the field separator????

Code:
awk -F\",\"

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

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 01-09-2012
Other way:
Code:
$ 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
This User Gave Thanks to birei For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Why awk print is strange when I set FS = " " instead of FS = "\t"?

Look at the following data file(cou.data) which has four fields separated by tab. Four fields are country name, land area, population, continent where it belongs. As for country name or continent name which has two words, two words are separated by space. (Data are not accurately... (1 Reply)
Discussion started by: chihuyu
1 Replies

4. Shell Programming and Scripting

sort, columns, no result! can I print files of "planes"?

hi, please can I ask you for some help? I have data from 3D situation, x y z value I'd like to use gnuplot to generate maps of the value in the planes z=0 to z=1 for example, my file looks like -0,012 0,0060 0,0 0,13972813076023477 -0,012 0,0064319163 4,2894483E-4 ... (1 Reply)
Discussion started by: kocour
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

awk statement to match all lines starting with "#"

Looking for awk statement that will match all lines starting with "# " if ( $1 == \^"#" ) Input file: # of the server. If you would like to set these, please take out the # pound (#) sign in front of one or all severities and set it equal to # severity desired. For example, FATAL=3 #... (2 Replies)
Discussion started by: Arsenalman
2 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

Results of "find" to an array

I am looking to search a directory for a folder or file and when it finds any hits I want it to store those hits in an array so I can work with those hits later on. I have been trying to do something like this but it isn't putting results into an array like I would like, is it syntax or is this... (8 Replies)
Discussion started by: tret
8 Replies

9. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question