简体   繁体   中英

How can I find and concatenate files that have varying characters but whose root is listed in a text file?

So I'm trying to concatenate files that have slightly varying prefixes and suffixes but have the same root. But I want to do it for all the "roots" listed in a text file.

So far I have been able to do this but by one root at a time which works fine:

#works
find directory/ -type f -name ‘*ftsW*.fna' -exec cat {} \; > new_directory/ftsW.fna

However, when I try to loop through all the roots listed in the 'roots.txt' file, i get empty files of all the roots listed in the txt file.

#outputs empty files

filename='roots.txt'
filenames=`cat $filename`
for a in $filenames
do find directory/ -type f -name '*$a*.fna' -exec cat {} \; > new_directory/$a.fna
done

Does anyone know I can fix this code? Thank you!

When you're running in the loop you write the first file to your destination, then you overwrite your dest with the next file, and then overwrite it again, etc. So you only end up with your last file. Try this:

cat $(find directory/ -type f -name '*$a*.fna') > new_directory/$a.fna

Which will cat all the files at once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM