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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find matching file in bash with variable file names but consisent prefixs
# 1  
Old 10-18-2016
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.

Code:
for file in /home/cmccabe/Desktop/comparison/missing/*.txt
do
    file1=${file##*/}    # Strip off directory
    getprefix=${file1%%_*.txt}
    file1="/home/cmccabe/Desktop/comparison/final/${file1%%_*.txt}_*.vcf"  # look for matching file
    if [[ -f "$file1" ]]

files with prefix -- directory 1
Code:
F113.txt
H456.txt

files searched for prefix
Code:
F113_epilepsy.vcf
H456_marfan.vcf

In the files above F113.txt matches F113_epilepsy.vcf and H456.txt matched H456_marfan.vcf

Currently, I am not getting a match returned presumably because the portion after the _ is variable, but I am not sure if the above bash will find the file using the *. Thank you Smilie.
# 2  
Old 10-18-2016
Filename expansion doesn't happen inside double-quotes or on assignments. Try changing:
Code:
    file1="/home/cmccabe/Desktop/comparison/final/${file1%%_*.txt}_*.vcf"  # look for matching file

to:
Code:
    file1=$(printf '%s\n' "/home/cmccabe/Desktop/comparison/final/${file1%%_*.txt}_"*.vcf) # look for matching file

Note that the asterisk that is being used as a filename matching character has been moved out of the quoted portion of the string, and the expansion is done in an argument to a utility (printf in this case) instead of in a string being assigned to a variable. Without the command substitution, you would be assigning a literal asterisk to the variable instead of expanding it to a pathname of an existing file (or files).

Last edited by Don Cragun; 10-18-2016 at 09:25 PM.. Reason: More clearly show changes.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-19-2016
What would your code need to do if there was more than one file that matches your patterns? The value of file1 could be list. Would that be a problem for you?



Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 10-19-2016
The wildcard globbing can match none, one, or several files.
Perhaps you don't need the wildcard globbing at all? The following simply strips off the .txt and appends .vcf
Code:
 file1="/home/cmccabe/Desktop/comparison/final/${file1%%.txt}.vcf"  # look for matching file

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 10-19-2016
Quote:
Originally Posted by MadeInGermany
The wildcard globbing can match none, one, or several files.
Perhaps you don't need the wildcard globbing at all? The following simply strips off the .txt and appends .vcf
Code:
 file1="/home/cmccabe/Desktop/comparison/final/${file1%%.txt}.vcf"  # look for matching file

Take a look at post #1 in this thread again. Using the above code won't allow the code to take the filenames:
Code:
F113.txt
H456.txt

in one directory and find the corresponding files:
Code:
F113_epilepsy.vcf
H456_marfan.vcf

in the other directory.

Using the code suggested in post #2 will have the if statement:
Code:
    if [[ -f "$file1" ]]

take the then branch if there is exactly one matching file and take the else branch (if there is one) if there are no matching files and if there is more than one matching file. But, of course, we weren't shown the then or else branches so we can't make an educated guess at whether or not matching multiple files is an issue that the code is prepared to handle.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 10-19-2016
Thank you all Smilie

Each filename prefix is unique and will only occur once. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract file names from file and set variable to 'highest' one

HI Folks - I have a requirement where I need to scan a text file for a list of files. The file, we'll called it, files.txt looks like such: inbox/EBS/Client_GL_Detail_PBCS_112517_SEP2017.txt inbox/EBS/Client_GL_Detail_PBCS_112617_NOV2017.txt ... (9 Replies)
Discussion started by: SIMMS7400
9 Replies

2. Shell Programming and Scripting

Matching the header of a .CSV file with dynamic field names

I have a .CSV file (frequency - weekly) whose header contains the year-week value in two of the columns which keeps changing every week. For an instance please see below. Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10,Column11,Column12,Column13,201420... (4 Replies)
Discussion started by: dhruuv369
4 Replies

3. UNIX for Dummies Questions & Answers

find command: names matching the expression

Hello all, I need to print directories using find command. The directories names contain date in the format YYYYMMDD or the name of directory is only the date format. I want print directories, which doesn't start with this date. E.g I have dirs like foo20120101 foo20120101foo 20120101foo... (1 Reply)
Discussion started by: satin1321
1 Replies

4. Shell Programming and Scripting

find specific file names and execute a command depending on file's name

Hi, As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two... (3 Replies)
Discussion started by: armando110
3 Replies

5. Shell Programming and Scripting

How to find complete file names in UNIX if i know only extention of file

Suppose I have a file which contains other file names with some extention . text file containt gdsds sd8ef g/f/temp_temp.sum yyeta t/unix.sum ghfp hrwer h/y/test.text.dat if then.... I want to get the complete file names, like for above file I should get output as temp_temp.sum... (4 Replies)
Discussion started by: panchal
4 Replies

6. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

7. Shell Programming and Scripting

Shell - Matching 3 letter file names

Hi I am running a shell script with bdf command and want to match all files with length 3 inside a specific partitions. How do i do that say for example if i want to list all files with length 3 in /home/jimmy partition, bdf /home/jimmy the output i need is xyx abc yyy amp ... (4 Replies)
Discussion started by: PrasannaKS
4 Replies

8. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

9. Shell Programming and Scripting

Bash Script duplicate file names

I am trying to write a housekeeping bash script. Part of it involves searching all of my attached storage media for photographs and moving them into a single directory. The problem occurs when files have duplicate names, obviously a file called 001.jpg will get overwritten with another file... (6 Replies)
Discussion started by: stumpyuk
6 Replies

10. UNIX for Dummies Questions & Answers

Variable assignment for file names.

I am trying to process error files in selected directories. I can count the files that are there and export the contents to a file for either emailing or printing. The next step is to move the files to a processed directory with the name changed to .fixed as the last extension. for file in... (2 Replies)
Discussion started by: jagannatha
2 Replies
Login or Register to Ask a Question