Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Globbing a filename and then saving to a variable

I’ve got some files in a directory with a standard format, I’m looking to use a txt file with part of the filenames to extend them through * then finally add on a .gz tag as an output

For example, a file called 1.SNV.111-T.vcf in my directory, I have 111-T in my txt file.

#!/bin/bash
while getopts f: flag
do
        case "${flag}" in
                f) file=${OPTARG};;
esac
done

while IFS="" read -r p || [ -n "$p" ]
do
        vcf="*${p}.vcf"

        bgzip -c ${vcf} > ${vcf}.gz

done < $file

This will successfully run bgzip but actually save the output to be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

‘*111-T.vcf.gz’

So adding .gz at the end has "deactivated" the * character, as pointed out by Barmar this is because there isn’t a file in my directory called 1.SNV.111-T.vcf.gz so the wildcard is inactivated, please can anyone help?

I’m new to bash scripting but I assume there must be some way to save the "absolute" value of my vcf variable so that once it has found a match the first time, it’s now a string that can be used downstream? I really cant find anything online.

>Solution :

The problem is that wildcards are only expanded when they match an existing file. You can’t use a wildcard in the filename you’re trying to create.

You need to get the expanded filename into the vcf variable. You can do it this way:

vcf=$(echo *"$p.vcf")
bgzip -c "$vcf" > "$vcf.gz"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading