Save input as text in directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save input as text in directory
# 1  
Old 03-09-2015
Save input as text in directory

I am having a little trouble with some things using the code below:

1.
Code:
printf "Enter variant: "; read variant

The user enters the variant and that value is used in the python script. However, I am not sure how to save that value inputed as a text file in a specific directory (C:\Users\cmccabe\Desktop\Python27\).

2. Not sure how save the text file like this "(NM_004004.4:user input value)"

3. I am hoping a python command can be used in the menu

4. Does the code look correct? Thank you Smilie.


Code:
Code:
 #!/bin/bash

menu() {
    printf "\n Welcome to annovar, please make a selection from the MENU \n
    ==================================\n\n
    \t 1  GJB2 analysis\n
    \t 2  MECP2 analysis\n
    \t 3  Phox2B analysis\n
    \t 4  Exit\n\n
    ==================================\n\n"

    printf "\t Your choice: "; read menu_choice

    case "$menu_choice" in
        1) gjb2 ;;
	2) mecp2 ;;
	3) phox2b ;;
	4) printf "\n Bye! \n\n"; exit ;;
        *) printf "\n Invalid choice."; sleep 2; menu ;;
    esac
}

gjb2() {
    printf "\n\n"
    printf "What is the id of the patient getting GJB2 analysis  : "; read id
    printf "Enter variant: "; read variant

    [ -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:'
    C:\Users\cmccabe\Desktop\Python27\python.exe C:\Users\cmccabe\Desktop\Python27\run_batch_job.py C:\Users\cmccabe\Desktop\Python27\${variant}.txt
    C:\Users\cmccabe\Desktop\annovar\${id}.txt PositionConverter
    parse
}


Last edited by cmccabe; 03-09-2015 at 04:33 PM..
# 2  
Old 03-09-2015
I'd suggest putting a loop in your menu, this avoids building up a new stack instance every time you call menu again from within an existing menu.

Also note that backslash is an escape character in bash and so would need to be escaped (either by enclosing in single quotes or by adding an additional backslash). More readable and acceptable for both windows and bash is to use forward slash.

Noe I just used printf to output then entered variant and redirected this to the NM_004004.4 file as requested.

See changes in red below.

Code:
#!/bin/bash

menu() {
    while true
    do
        printf "\n Welcome to annovar, please make a selection from the MENU \n
        ==================================\n\n
        \t 1  GJB2 analysis\n
        \t 2  MECP2 analysis\n
        \t 3  Phox2B analysis\n
        \t 4  Exit\n\n
        ==================================\n\n"

        printf "\t Your choice: "; read menu_choice

        case "$menu_choice" in
            1) gjb2 ;;
        2) mecp2 ;;
        3) phox2b ;;
        4) printf "\n Bye! \n\n"; exit ;;
        *) printf "\n Invalid choice."; sleep 2 ;;
        esac
    done
}

gjb2() {
    printf "\n\n"
    printf "What is the id of the patient getting GJB2 analysis  : "; read id
    printf "Enter variant: "; read variant

    printf "%s\n" "$variant" > c:/Users/cmccabe/Desktop/NM_004004.4

    [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
    [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

    cd 'C:'
    C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/${variant}.txt
    C:/Users/cmccabe/Desktop/annovar/${id}.txt PositionConverter
    parse
}

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-10-2015
Although absolute addresses are a good idea you could also give yourself a fighting chance and point directly to your Desktop:-

cd 'C:/Users/cmccabe/Desktop'

OR alternatively, assuming you are using CygWin:-

cd /cygdrive/c/Users/cmccabe/Desktop

As this eliminates any typo errors on multiple lines of this common directory pointer.

And call say python as:-

Python27/python.exe <your stuff here>

The only thing is that on Windows be aware of where Python[27] is installed as by default is sits in the Windows eqivalent of the 'root' drawer.

That is C:\Python27

Hope this helps too...

Bazza.
This User Gave Thanks to wisecracker For This Post:
# 4  
Old 03-10-2015
I am trying to open the bashpasrse.sh and have created bat file in windows.

Code:
Code:
 @echo off
set PATH=C:\cygwin\bin\;%PATH%@
    c:\cygwin\bin\bash.exe c:\cygwin\home\cmccabe\bashparse.sh

I have a similar batch that works fine but for some reason this does not. I am testing the code in bits so hopefully the gjb2 line works, but the others do not. Thank you Smilie.
# 5  
Old 03-10-2015
Nevermind, I must have typed something wrong.... its running now. However, I get this error:

Code:
 
Welcome to annovar, please make a selection from the MENU

        ==================================


                 1  GJB2 analysis

                 2  MECP2 analysis

                 3  Phox2B analysis

                 4  Exit


        ==================================

         Your choice: 1


What is the id of the patient getting GJB2 analysis  : GJB-1
Enter variant: c.203G>A
usage: run_batch_job.py [-h] [-v] [-b BUILD] INPUT OUTPUT TYPE
run_batch_job.py: error: argument INPUT: can't open 'C:/Users/cmccabe/Desktop/Py
thon27/c.203G>A.txt': [Errno 22] invalid mode ('r') or filename: 'C:/Users/cmcca
be/Desktop/Python27/c.203G>A.txt'
c:\cygwin\home\cmccabe\bashparse.sh: line 38: C:/Users/cmccabe/Desktop/annovar/G
JB-1.txt: No such file or directory

I can see that the c.203G>A.txt is not created in the directory, but I thought it should? Thanks.

After the user inputs the variant (c.203G>A), that as well as "NM_004004.4:" are saved to the
Code:
 c:/Users/cmccabe/Desktop/Python27

directory.

It looks like the NM_004004.4 is being printed to the desktop, but a file is not being saved that combines the input with that text.

Code:
 printf "%s\n" "$variant" > c:/Users/cmccabe/Desktop/NM_004004.4

I'm not sure how to do this. Thank you Smilie.

---------- Post updated at 10:27 AM ---------- Previous update was at 09:32 AM ----------

I modified the code a bit to try and add the text "NM_004004.4" to the GJB-2.txt using:

Code:
 sed -i '$a NM_004004.4' $id.txt

The GJB-2.txt is the $id and gets created using:
Code:
 printf "%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt

However, eventhough the file is there in the directory I get this error:

Code:
 What is the id of the patient getting GJB2 analysis  : GJB-2
Enter variant: c.204G>A
sed: can't read GJB-2.txt: No such file or directory

Thank you Smilie.

Last edited by cmccabe; 03-10-2015 at 11:42 AM..
# 6  
Old 03-10-2015
What if you enter a variant NOT containing the ">" char?
# 7  
Old 03-10-2015
I didn't think of that, thats a great point. But since the user is inputting the variant I think this line:

Code:
 printf "%s\n" "$variant" > c:/Users/cmccabe/Desktop/Python27/$id.txt

justs saves the variant information typed in by the user to the $id.txt

Then, this code: is supposed to add "NM_004004.4:" in front of the variant (NM_004004.4:c.204C>T or NM_004004.4:c.206insGGTT)

Code:
 sed -i '$a NM_004004.4:' c:/Users/cmccabe/Desktop/Python27/$id.txt

Is this right? Thank you Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select and save file in new directory

I am trying to select a file in bash and save it to a directory. The below does run but no selected file is saved. Thank you :). bash # select file printf "please select a file to analyze with entered gene or genes \n" select file in $(cd... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Input password to bash script, save, and enter when needed

I am looking for a way to start a script and have it prompt for a password that will be used later on in the script to SSH to another host and to SFTP. I don't want the password to be hard coded. Below is my script with the actual IP's and usernames removed. #!/usr/bin/expect -f... (2 Replies)
Discussion started by: jbrass
2 Replies

3. Shell Programming and Scripting

Use text of one file to save in another

I am trying to use the text of one file as a text file name with and the text of another as the contents of that text file. Is this possible? Thank you :). For example, in the two files attached, target.txt has: 1.txt 2.txt and out_parse.txt has: 13 20763642 20763642 C G... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

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 :). ... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

Save files in directory as txt

wget -x -i link.txt The above downloads and create unique entries for the 97 links in the text file. However, each new file is saved as CM080 with a FILE extention. Is there a way to convert each file in that directory to a .txt? The 97 files are in... (12 Replies)
Discussion started by: cmccabe
12 Replies

6. Programming

Save output in text C++

Hi , i want to save the output of my c ++ code to a text file which is in a particular path : this is part of my code and I dunno where I am doing it wrong do { for( int i = 0; i < l; ++i ) { std::cout << 1 + k * i + index << ' '; } ... (2 Replies)
Discussion started by: siya@
2 Replies

7. Shell Programming and Scripting

Get the input from user and save it as .txt file

Hi friends, I am pretty new to shell scripting, please help me in this Scenario. for example, If I have one file called input.txt once I run the script, 1.It has to delete the old input.txt and create the new input.txt (if old input.txt is not there, no offence, just it has to create a... (2 Replies)
Discussion started by: Padmanabhan
2 Replies

8. 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

9. Shell Programming and Scripting

How to add data from 2 input files and save it in 1 output file

Hi, i have 2 input files which are file1.txt and file2.txt. I need to extract data from file1.txt and file2.txt and save it in file3.txt like example below:- File1.txt ID scrap1 Name scrap1 start 1 end 10 ID scrap2 Name scrap2 start 11 end ... (4 Replies)
Discussion started by: redse171
4 Replies

10. Hardware

<power save [ input 1 ]>

hello on my ultra 5 (under debian) obp since I activated an error saving mode display. Since it starts and displays <power save >, then this is prologue to sleep for hours. i can ejecting the cdrom works, I hear the fans .... I have not access of OpenBoot (stop + a). can i do an hard... (0 Replies)
Discussion started by: philo_71
0 Replies
Login or Register to Ask a Question