Add input to text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add input to text file
# 1  
Old 12-02-2014
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 Smilie.

Last edited by cmccabe; 12-02-2014 at 02:14 PM..
# 2  
Old 12-02-2014
or maybe:

Code:
 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
Code:
 _matched.avinput

to the output. Thank you Smilie.
# 3  
Old 12-02-2014
Instead of 'while read line', just 'cat filename'.

Please post your script in code tags.
# 4  
Old 12-02-2014
The entire script:

Code:
 #!/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

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

The desired output in file.txt would be:

12345_matched.avinput
67890_matched.avinput

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

Thank you Smilie.
# 5  
Old 12-02-2014
Considering I don't know what the contents of filename are, I don't have the information needed to help you.
# 6  
Old 12-02-2014
Did post 4 help? Thank you ver much Smilie.
# 7  
Old 12-02-2014
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search last column of INPUT.txt in TABLEs text and add correspond columns to INPUT.txt

Hi dears i use bash shell i have INPUT.txt like this number of columns different in one some row have 12 , some 11 columns see last column INPUT.txt CodeGender Age Grade Dialect Session Sentence Start End Length Phonemic Phonetic 63 M 27 BS/BA TEHRANI 3 4 298320 310050... (2 Replies)
Discussion started by: alii
2 Replies

2. Shell Programming and Scripting

Copying a file to multiple other files using a text file as input

Hello, I have a file called COMPLIST as follows that contains 4 digit numbers.0002 0003 0010 0013 0015 0016 0022 0023 0024 0025 0027 0030 0031 0032 0033 0035 0038 0041 (3 Replies)
Discussion started by: sph90457
3 Replies

3. Shell Programming and Scripting

Open Text file input data and save it.

Hi Guys, I have blank file A.txt I will run the script xyz.sh First i want to open a.txt file... Now i will enter some data like XYZ ABC PQR .. Save it and keep continue my script.... END of my script. Thanks (1 Reply)
Discussion started by: asavaliya
1 Replies

4. Shell Programming and Scripting

Read text file and use it as input

I need to take a text file that holds a bunch of data and run each the stuff in it as an input for the program. the file would hold stuff like this: thing1.awesomesite.com 80 123.456 thing2.awesomesite.com 80 789.098 thing3.awesomesite.com 80 765.432 ... Now I already know the... (1 Reply)
Discussion started by: shade917
1 Replies

5. Shell Programming and Scripting

help with using text file as input

Hello All, I'm attempting to use a text file as input to a specific field in a command. Below is the command... Typically it looks like this ans_dump testzone.com channel=dnsw32 | grep AAAA I have about 500 zones I want to check... how do I use my text file as input where the zone name... (2 Replies)
Discussion started by: spartan22
2 Replies

6. Shell Programming and Scripting

input text at certain lines of a file

Hi, I have an xml file which will be edited by the user. I would like to get input from user and insert that at line 40 of the xml file. PLease can some one help me to know how to insert the text at specified line using shell script. (6 Replies)
Discussion started by: sunrexstar
6 Replies

7. Shell Programming and Scripting

input text from file into 2d array

Hi all I have a little brainscratcher here. I want to draw a pie chart from data in a text file. The drawing of the graph works fine, if I insert the data manually into a 2d array. Now I want to pull the data from a text file (which was created using a uniq -c command) see sample below.... (2 Replies)
Discussion started by: pietie
2 Replies

8. UNIX for Dummies Questions & Answers

add new lines of text before and after each input line

I have a file that contains hundreds of lines such as: this_is_macro,000001 this_is_macro,000002 this_is_macro,000003 I would like to add the variable words MACROBEGIN MACRO_000001 MACROBEGIN MACRO_000002 MACROBEGIN MACRO_000003 above each line and add the word MACROEND ... (2 Replies)
Discussion started by: kenneth.mcbride
2 Replies

9. Shell Programming and Scripting

Replace text in input file

I wish to replace values of specific parameters in an input file for batch runs of a java code. It's essentially a nested for-loop sorta like this: valuearray1 contains values for param1 valuearray2 contains values for param2 for (all values in valuearray1) go into specific position in... (2 Replies)
Discussion started by: daphantomica
2 Replies

10. Shell Programming and Scripting

input text into file

hello everyone, this is my first time posting here so be nice ;-) I am a bit new at unix scripting and have basically been hacking other peoples scripts to get them to do what I need. I have now hit a bit of a stop. This problem is very basic but I can't just seem to figure out how to get... (1 Reply)
Discussion started by: cannonfodder
1 Replies
Login or Register to Ask a Question