Multiple input and save in windows format

The below code works great if the user inputs a single value. The sed command applies the NM_ with the user input it is saved to a file. However, if two values are entered the below does not work. Can both values be saved at the same time if they are entered in windows format? Thank you :).

 gjb2() {
    printf "\n\n"
    printf "What is the id of the patient getting GJB2 analysis  : "; read id
    printf "Enter variant(s): "; read variant
	
	[ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return
	
	# save file in windows python directory
    printf "%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt
	
	# add transcript
	sed -i '$a NM_004004.5:' c:/Users/cmccabe/Desktop/Python27/$id.txt
    printf "NM_004004.5:%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt
	add2text ${id}.txt
	gjb2name
}	

Maybe:

 awk 'NR!=FNR{print L[FNR],$0; next} $1=="variant"{I=$2}
    $1=="Name"{L[++p]=I FS $2}' $variant $variant > $id.txt 
 user enters: c.74G>A

1 value output
NM_004004.5:c.74G>A

user enters:c.79G>A,c.283G>A

current 2 value output
NM_004004.5:c.74G>A,c.283G>A

Desired 2 value output
NM_004004.5:c.74G>A
NM_004004.5:c.283G>A 

That depends on the shell you are using. In a recent bash try read ing into an array:

IFS="," read -a TArr <<<"c.79G>A,c.283G>A"
echo ${#TArr[@]}
2
echo ${TArr[@]}
c.79G>A c.283G>A

and loop through the array.

BTW, help me out - what are your intentions here:

                   printf "%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt
# add transcript     sed -i '$a NM_004004.5:' c:/Users/cmccabe/Desktop/Python27/$id.txt
       printf "NM_004004.5:%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt 

You create a file, the transcript is commented out, and then you overwrite the newly created file? Quite misleading for the reader ... I'd propose to get your act together and streamline the script.

 printf "%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt
# add transcript sed -i '$a NM_004004.5:'c:/Users/cmccabe/Desktop/Python27/$id.txt
printf "NM_004004.5:%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt 

The user inputs the variant and the sed command appends the transcript at the end (after the variant) So like this c.79G>ANM_004004.5:

the printf re-orders that to NM_004004.5:c.79G>A and saves that file in the python directory. Probably not the most efficient, but I will take your advice and work on it.

Out of curiosity do you find more scientists, especially genomic specialists, learning to program. Just curious because the data is huge and bioinformatics is becoming more important. Thank you :).

---------- Post updated at 01:57 PM ---------- Previous update was at 01:42 PM ----------

Maybe:

 gjb2() {
    printf "\n\n"
    printf "What is the id of the patient getting GJB2 analysis  : "; read id
    printf "Enter variant(s): "; read variant
	
	[ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return
	
    printf "NM_004004.5:%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt
while true
do
           IFS="," read -a TArr <<<"$variant"
           echo ${#TArr[@]}
           2
           echo ${TArr[@]}
           $variant
done
	add2text ${id}.txt
	gjb2name
}	

Thank you :).

The printf "NM_004004.5:%s\n" ... does NOT re-order anything, it OVERWRITES the former file, destroying any work done on it before.

BTW - the sed line is commented out.

Yes, we do see quite some biological questions in these forums, dealing with large files...

I removed the sed line as it is not necessary but the below code is not correct is it:

  gjb2() {
    printf "\n\n"
    printf "What is the id of the patient getting GJB2 analysis  : "; read id
    printf "Enter variant(s): "; read variant
 
 [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return
 printf "NM_004004.5:%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt

while true
do
           IFS="," read -a TArr <<<"$variant"
           echo ${#TArr[@]}
           2
           echo ${TArr[@]}
           $variant
done
 add2text ${id}.txt
 gjb2name
} 

If the user inputs c.79G>A,c.283G>A

then in the file created:
NM_004004.5:c.79G>A
NM_004004.5:c.283G>A

Thank you :).

No. Try sth. like (untested)

        printf "Enter variant(s): "; IFS="," read -a variant
        
        [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

        for ((i=0; i<${#variant[@]}; i++))
                do printf "NM_004004.5:%s\n" ${variant[$i]} >> c:/Users/cmccabe/Desktop/Python27/$id.txt
        done

You may need to remove the target file before the loop.

1 Like

That worked great. Thank you :).