Use same file selected in first bash process that has matching digits in it fot the second


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use same file selected in first bash process that has matching digits in it fot the second
# 1  
Old 07-23-2016
Use same file selected in first bash process that has matching digits in it fot the second

In the below portion of a bash script the user selects a file from a directory.

Code:
select file in $(cd /home/cmccabe/Desktop/NGS/API/5-14-2016/bedtools;ls);do break;done

Code:
files in directory

123_base_counts.txt
456_base_counts.txt
789_base_counts.txt

second portion of bash currently (user manually selects the file)

Code:
select file in $(cd /home/cmccabe/Desktop/NGS/API/5-14-2016/annovar;ls);do break;done

What I am trying to do is if the user selects 123_base_counts.txt in the first select, then in the second select whatever the name of the file in the annovar directory, the one that starts with 123 is used. The files will always have a _ in them. However my attempt seems to have flaws in it that I am not sure how to fix. Thank you Smilie.

The directory in the second select has files names:

Code:
123_variant_strandbias_readcount.vcf.hg19_multianno_removed_final (this one is automatically selected because it has the same starting digits as the original file)
456_variant_strandbias_readcount.vcf.hg19_multianno_removed_final
789_variant_strandbias_readcount.vcf.hg19_multianno_removed_final

Bash attempt
Code:
printf "please select a file to analyze with a panel \n"
select file in $(cd /home/cmccabe/Desktop/files;ls);do break;done # specify file
            echo $file
for file in /home/cmccabe/Desktop/files/$file; do
     bname=$(basename $file)
     pref=${bname%%.txt}
     done
printf "filtering vcf"
printf "\n\n"
nameNumeric=$(basename $file | awk -F'_' '{print $1}');
secondFile=$(ls /home/cmccabe/Desktop/files/${nameNumeric}*);
            echo $file
for file in /home/cmccabe/Desktop/annovar/$file; do
echo $file 
printf "will be filtered"
done

current output:
please select a file to analyze with a panel 
1) 123_base_counts.txt
2) 456_base_counts.txt
3) 789_base_counts.txt
#? 1
123_base_counts.txt
filtering vcf

/home/cmccabe/Desktop/files/123_base_counts.txt
/home/cmccabe/Desktop/annovar//home/cmccabe/Desktop/files/123_base_counts.txt

I can manually select each file but I am not sure how to automate the selection. Thank you Smilie.

Last edited by cmccabe; 07-23-2016 at 01:56 PM.. Reason: fixed format
# 2  
Old 07-23-2016
Hi - This is only rough but something like the below should work on your data:

Code:
#!/bin/bash
FILESDIR=/home/cmccabe/Desktop/files
ANNOVARDIR=/home/cmccabe/Desktop/annovar

PS3="please select a file to analyze with a panel: "
select file in ${FILESDIR}/*; do
        [[ -z ${file} ]] && printf "Pick a file" || break
done

filename=${file##*/}
file2=$(ls ${ANNOVARDIR}/${filename%%_*}*)
printf "File 1 is: ${file}\n"
printf "File 2 is: ${file2}\n"

This makes the assumption that:
  • There is only 1 file in the 'annovar' directory which has the same prefix (delimited by an underscore) as the associated file in the 'files' directory and it exists
  • This is not just applicable for '123_' but for all files
This User Gave Thanks to pilnet101 For This Post:
# 3  
Old 07-23-2016
That's a great start and close but I cannot seem to get the right syntax for basename .

Currently, the user gets:

Code:
1) /home/cmccabe/Desktop/files/123_base_counts.txt
2) /home/cmccabe/Desktop/files/456_base_counts.txt
3) /home/cmccabe/Desktop/files/789_base_counts.txt
please select a file to analyze with a panel:

If I didn't need the path before the filename where would basename be added?:

Code:
FILESDIR=/home/cmccabe/Desktop/files
ANNOVARDIR=/home/cmccabe/Desktop/annovar

PS3="please select a file to analyze with a panel: "
select file in ${FILESDIR}/*; do
bname=$(basename $file)
done

filename=${file##*/}
file2=$(ls ${ANNOVARDIR}/${filename%%_*}*)
printf "File 1 is: ${file}\n"
printf "File 2 is: ${file2}\n"

desired
Code:
1) 123_base_counts.txt
2) 456_base_counts.txt
3) 789_base_counts.txt
please select a file to analyze with a panel:

Thanks again Smilie

Last edited by jim mcnamara; 07-23-2016 at 10:24 PM.. Reason: added details code tag completion
# 4  
Old 07-23-2016
Sorry about that, I've amended the script slightly so it should work as expected now:

Code:
#!/bin/bash
FILESDIR=/home/cmccabe/Desktop/files
ANNOVARDIR=/home/cmccabe/Desktop/annovar

PS3="please select a file to analyze with a panel: "
select file in $(cd ${FILESDIR};ls *); do
        [[ -z ${file} ]] && printf "Pick a file" || break
done

file1=${FILESDIR}/${file}
file2=$(ls ${ANNOVARDIR}/${file%%_*}*)
printf "File 1 is: ${file1}\n"
printf "File 2 is: ${file2}\n"

It looks like basename isn't really required as you are dealing with the file names themselves straight from the select loop. Then when you create the file1 variable you append the directory in front of it.
This User Gave Thanks to pilnet101 For This Post:
# 5  
Old 07-23-2016
works perfect.... thank you for your help and explanations, I really appreciate them Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Bash lookup matching digits for secong file

In the bash below the user selects the file to be used. The digits of each file are unique and used to automatically locate the next file to be used in the process. The problem I can not seem to fix is that the full path needs to be referenced in the second portion and it is not currently. Is... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

Bash detecting number of digits in line

Hi I have a problem, I am attempting to write a bash script that goes through a file and can determine how many characters are at a set point in a line starting with QTY+113:100:PCE, If it detects 3 digits (number in bold) then pad it out with 12 zero's If there are only two digits then pad it... (8 Replies)
Discussion started by: firefox2k2
8 Replies

5. Shell Programming and Scripting

Selected matching lines

two files: one with the line number only, and the 2nd one with line number and content, as following: line_file.txt 1 3 5 9 23 30 content_file.txt 1|we are the world|good|great 2|easily do this by highlighting you|easily do this by highlighting you|easily do this by highlighting... (2 Replies)
Discussion started by: dtdt
2 Replies

6. Shell Programming and Scripting

How to pass enter key or selected character in bash script?

hi, i've bash script thats working... but now i need to add a line....that prompts for user input....like yes and 1 as complete install.... so here's how it looks... $ cd 9200 (cd into directory) $./install (hv to type ./install to run install then ask for) ----do you want to... (4 Replies)
Discussion started by: kernel11
4 Replies

7. Shell Programming and Scripting

BASH: remove digits from end of string

Hi there, im sure this is really simple but i have some strings like this e1000g123001 e1000g0 nge11101 nge3and i want to create two variables ($DRIVER and $INSTANCE). the first one containing the alpha characters that make up the first part of the string, e.g. e1000g or nge and the... (9 Replies)
Discussion started by: rethink
9 Replies

8. UNIX Desktop Questions & Answers

matching 3 digits at the begining and the end of the line

I have a file with hundreds of records and I need to find those records that have three digits at the beginning and the same three digits at the end. $GREP '\(\)\(\)\(\)\3\2\1'I believe this is part of the script but I am not sure how to compare these 3 digits with the 3 digits at the end of... (2 Replies)
Discussion started by: bartsimpsong
2 Replies

9. Shell Programming and Scripting

bash: closing file descriptors from a process

Below is a test script to illustrate a problem from a larger script I am writing. $ cat /tmp/loggingtest #!/bin/bash lvcreate -s -l 100%FREE -n var-data-snapshot vg00/var-data 2> >(logger -t "loggingtest.crit") 1> >(logger -t "loggingtest.info") sync & wait lvremove -f... (1 Reply)
Discussion started by: jelloir
1 Replies

10. Shell Programming and Scripting

Process selected lines

I have an if statement where I state that if there are more than one records (lines) found containing a string in a file, then it enters into a while loop to use each line for as many lines as there are and then stop. Trouble is, I can't figure out how to move to the next instance of each line. ... (2 Replies)
Discussion started by: derekphl
2 Replies
Login or Register to Ask a Question