Add input to text file

In the attached bash file I am trying to add a block of code to add2text that will copy the input from match to a text file (file.txt).

For example, if from the menu choice 1 is select the user is asked for the id.... lets say that is 12345, after that id is matched and converted can it be added to a text file in a directory. The name will always be input id{id}_matched.avinput. There may be multiple id's but the .avinput will be the same.

My attempt at this is in the attachment as well. Thank you :).

or maybe:

 add2text() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s\n," $id
		cd 'C:\Users\cmccabe\Desktop\annovar'
		while read line; do echo -e "$line\n"; done < file.txt
    esac 
} 

Not sure how to add the

 _matched.avinput 

to the output. Thank you :).

Instead of 'while read line', just 'cat filename'.

Please post your script in code tags.

The entire script:

 #!/bin/bash
menu() {
    printf "\n MENU \n
    ==================================\n\n
    \t 1  Match patient\n
    \t 2  Sanger analysis\n
    \t 3  Batch analysis\n
    \t 4  Individual analysis\n
    \t 5  Supplemental analysis\n
    \t 6  Exit\n\n
    ==================================\n\n"
    printf "\t Your choice: "; read menu_choice
    case "$menu_choice" in
        1) match ;;
  2) sanger ;;
  3) batch ;;
  4) individual ;;
  5) supplemental ;;
        6) printf "\n Bye! \n\n"; exit ;;
        *) printf "\n Invalid choice."; sleep 2; menu ;;
    esac
}
match() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
    printf "What is the id of the patient to be matched  : "; read id
    printf "What panel: "; read panel
    [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && menu
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && menu
    cd 'C:\Users\cmccabe\Desktop\annovar'
    OMR=Output_Mutation_Report
    perl -aF/\\t/ -lne 'BEGIN{%m=map{chomp;s/\cM|\cJ//g;$p=join("\t",(split/\t/)[4,5]);($p,$_)} <>;$m{"#CHROM\tINFO"}=$m{"Chr\tSegment Position"}};/SEGPOS=(\d+)/ || /\t(INFO)\t/ or next;$p=$F[0]."\t".$1;exists $m{$p} and print join("\t",$_,$m{$p})' ${id}_${panel}_${OMR}.txt < ${id}_${panel}_${OMR}_Filtered.vcf > ${id}_matched.vcf
    convert
}
convert() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
    printf "Does the file need to be converted? Y/N "; read convert_choice
    
    case "$convert_choice" in
        [yY]) perl convert2annovar.pl -includeinfo -format vcf4old ${id}_matched.vcf > ${id}_matched.avinput
        additional ;;
        [nN]) additional ;;  
        *) convert ;;
    esac
}
add2text() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
  cd 'C:\Users\cmccabe\Desktop\annovar'
  while read line; do echo -e "$line\n"; done < file.txt
    esac
}
additional() {
    printf "\n\n"
    printf "Are there additonal patients to be matched?  Y/N "; read match_choice
    case "$match_choice" in
        [yY]) id=""; panel=""; match ;;
        [nN]) id=""; panel=""; menu ;;  
        *) additional ;;
    esac
}
sanger() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s\n" $id
    printf "What is the name of the patient to have sanger annotation  : "; read id
    [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && menu
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && menu
    cd 'C:\Users\cmccabe\Desktop\annovar'
              $( perl table_annovar.pl ${id}.txt humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo )
    additionalsanger
}
additionalsanger() {
    printf "\n\n"
    printf "Are there additonal sanger patients?  Y/N "; read match_choice
    case "$match_choice" in
        [yY]) id="";  sanger ;;
        [nN]) id=""; panel=""; menu ;;  
        *) additional ;;
    esac
}
batch() {
    printf "\n\n"
    printf "How many patients  : "; read id
    [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && menu
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && menu
    cd 'C:\Users\cmccabe\Desktop\annovar'
              $( perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt )
     printf "Are there additional patients  : "; read id
  case "$match_choice" in
        [yY]) id=""; individual ;;
        [nN]) id=""; panel=""; menu ;;
  *) individual ;;
    esac
}
individual() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
    printf "What is the id of the patient  : "; read id
    printf "What panel: "; read panel
    [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && menu
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && menu
    cd 'C:\Users\cmccabe\Desktop\annovar'
              $( perl table_annovar.pl ${id}_matched.avinput humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo )
    printf "Are there additional patients  : "; read id
  case "$match_choice" in
        [yY]) id="";  individual ;;
        [nN]) id=""; panel=""; menu ;;
  *) additional ;;
    esac
}
supplemental() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
    printf "What is the id of the patient  : "; read id
    printf "What panel: "; read panel
    [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && menu
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && menu
    cd 'C:\Users\cmccabe\Desktop\annovar'
              $( perl table_annovar.pl ${id}_matched.avinput humandb/ -buildver hg19 -protocol refGene,popfreq_all,exac02,1000g2014oct_all,KAT6B,SPRED1,NF1 -operation g,f,f,f,f,f,f -otherinfo )
    printf "Are there additional patients  : "; read id
  case "$match_choice" in
        [yY]) id="";  individual ;;
        [nN]) id=""; panel=""; menu ;;
  *) additional ;;
    esac
}
# actual start of this program
menu # run menu function 
 The id is the read id variable in the match section and the _matched.avinput comes from the convert section 

I am not sure I follow the

 cat filename 

The desired output in file.txt would be:

12345_matched.avinput
67890_matched.avinput

 where 12345 and 67890 were inputted by the user in the matched script and the _matched.avinput resulted from the convert. 

Thank you :).

Considering I don't know what the contents of filename are, I don't have the information needed to help you.

Did post 4 help? Thank you ver much :).

Not really. It implies the input is already what you want, which case, cat should print it directly.

If it's not, tell me what it is, not just what you want it to be.

[CODE} Currently what is done is if there are multiple id's entered in the match section, say 123, 456, 789 then they are manualy copied to the file.txt with the _matched.avinput typed in. [/CODE]

 file.txt resides in:
cd 'C:\Users\cmccabe\Desktop\annovar' 
 So in file.txt:
123_matched.avinput
456_matched.avinput
789_matched.avinput 

Does this help? Thank you :).

So the input file contains: 123, 456, 789 ?

Yes, but the id can be variable.

Thank you for your help :).

Does this help:

menu -> match -> convert? -> NO -> add2text -> additional? -> NO -> menu
^ | | |
| +--> YES --+ +--> YES +
| |
+------------------------------------------------+
add2text (manually inputed in match section (read id variable)
after convert (${id}_matched.avinput) - this text in file.txt 

[/FONT][/COLOR][/SIZE]

[/FONT][/SIZE][/FONT]

I don't get it.
Do you want to
read from file.txt
or
write to file.txt
?

write to file.txt, I apologize for the confusion and appreciate your help :).

NB < file.txt is read, > file.txt is write!
You probably want this:

convert() {
    printf "\n\n"
    printf "DEBUG INFO: VALUE OF \$id: %s, VALUE OF \$panel: %s\n" $id $panel
    printf "Does the file need to be converted? Y/N "; read convert_choice
    
    case "$convert_choice" in
        [yY]) perl convert2annovar.pl -includeinfo -format vcf4old ${id}_matched.vcf > ${id}_matched.avinput
        add2text ${id}_matched.avinput
        additional ;;
        [nN]) additional ;;  
        *) convert ;;
    esac
}
add2text() {
  # $1 is the first argument to add2text
  cd 'C:\Users\cmccabe\Desktop\annovar'
  echo "$1" > file.txt
}

Your program has a bad structure. E.g. convert is a function that is expected to return. But your functions call each other or themselves without ever reaching their closing } , so the return stack grows and grows.
Hopefully the program is finished by the user before it runs out of stack space!

I plan on re-writing the code eventually, when I have time to contain a

 loop structure 

. The input files as of now are ~100 kb so I hope it is good for a little bit. Do you have an idea of how many files could be processed on an 8 GB windows 7 computer? I am the only bioinformatics individual at the hospital and time is very constrained right now.

The code seems to write to text, however if there are multiple entries, only the last one appears in file.txt.
For example, 123, 456, 789 (3 seperate entries) and only 789 appears in file.txt

Does the

 echo "$1" need to be changed? 

Thank you :).

Usually data subject to this sort of editing is put in a RDBMS table so it is all equally accessible, changes can be logged, standard tools allow editing. I have my wife updating the Christmas Card list in a LibreOffice odb file with one table. Sort, insert, delete, update are all trivial here.

There are actually two types of writing to a file:
> is overwrite, and
>> is append-write.
Use the latter!

I modified the code a little to allow for multiple entries, but it seems to append to the existing in file.txt.

For example, if file.txt has
000
001
002 in it already currently it just adds the new entries to these 3. So file.txt now has:
000
001
002
123
456
789 in it.

Is there a way that the old data can be replaced with the new? That is, 000, 001, 002 are replaced with the new 123, 456, 789? If not, no worries the code works great. Thank you :).

 add2text() {
  # $1 is the first argument to add2text
  cd 'C:\Users\cmccabe\Desktop\annovar'
  echo "$1" >> file.txt
} 

You can empty the file (overwrite with nothing) with

> file.txt

Do that at a reasonable point e.g. before you call the

menu
1 Like

You can change any line with "sed -i", which is much like vi/vim in : mode (ed/ex).

I added

 > file.txt 

before the menu but I must not have understood. I attachd the bash as well as file.txt. Thank you for all your help :).