awk if then else statement

I am using awk as part of and if then else statement. I am trying to have the user enter a gene name and then a variant or variants and have a specific transcript assigned to the variants depending on the gene. Below is my code but the if then else statement is wrong. Basically, the gene name would be the condition that assigns a particular transcript to a variant.

The desired output would be, if the user enters GJB2 as the gene name and then c.283G>A for the variant there is a statement for that condition that saves a file with NM_004004.5:c.283G>A . However if MECP2 is entered as the gene name and c.48delC,c.255G>T are entered as the variants, the a file is saved:

 
NM_004992.3:c.48delC
NM_004992.3:c.255G>T 

Thank you :).

 syntax() {
    printf "\n\n"
    printf "Please enter the gene name  : "; read gene
    printf "Enter variant(s): "; IFS="," read -a variants
        
        [ -z "$gene" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$gene" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

		awk '{ 
		if(${gene} = "GJB2")
        for ((i=0; i<${#variants[@]}; i++))
              do printf "NM_004004.5:%s\n" "${variants}" >> c:/Users/cmccabe/Desktop/Python27/$gene.txt >> c:/Users/cmccabe/Desktop/Python27/out.txt;
	    done
		else if(${gene} = "MECP2")
        for ((i=0; i<${#variants[@]}; i++))
		      do printf "NM_004992.3:%s\n" "${variants}" >> c:/Users/cmccabe/Desktop/Python27/$gene.txt >> c:/Users/cmccabe/Desktop/Python27/out.txt;
        done
		else if(${gene} = "Phox2B")
        for ((i=0; i<${#variants[@]}; i++))
		      do printf "NM_003924.3:%s\n" "${variants}" >> c:/Users/cmccabe/Desktop/Python27/$gene.txt >> c:/Users/cmccabe/Desktop/Python27/out.txt 
	    }'
	name
} 

From the syntax of your read statement, I'm assuming you're using bash... But, in bash and in ksh , the way to reference then nth element of an array is ${array[$n]} ; using ${array} just gives you the 0 th element of the array.

Shell variables (whether they are scalars or arrays) can't be referenced by name in an awk script.

You can't write (overwrite or append) to two files using redirection in the shell or in awk . Either call printf twice (redirecting the output of each to one of your files), or use tee to make a copy in one file while directing the output to the other file.

Why would you invoke awk when there are no calculations being performed; why not just use shell if , for , and printf statements?

I'm surprised that you don' t want to print some kind of diagnostic if the entered gene name is not an empty string is not end , and is not one of your three expected gene names, but this seems to do something similar to what I think you were trying to do:

syntax() {
	printf "\n\n"
	printf "Please enter the gene name  : "; read gene
	[ -z "$gene" ] &&
		printf "\n No ID supplied. Leaving match function." &&
		sleep 2 &&
		return
	[ "$gene" = "end" ] &&
		printf "\n Leaving match function." &&
		sleep 2 &&
		return
	printf "Enter variant(s): "; IFS="," read -a variants

	code=""
	if [ "$gene" = "GJB2" ]
	then	code="NM_004004.5"
	elif [ "$gene" = "MECP2" ]
	then	code="NM_004992.3"
	elif [ "$gene" = "Phox2B" ]
	then	code="NM_003924.3"
	else	return
	fi
	for ((i = 0; i < ${#variants[@]}; i++))
	do	printf "%s:%s\n" "$code" "${variants[$i]}"
	done | tee -a c:/Users/cmccabe/Desktop/Python27/$gene.txt \
			>> c:/Users/cmccabe/Desktop/Python27/out.txt
}
2 Likes

Thank you :). I didn't even think about

, but that is a good point that I will build in as well. Thanks.