0

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:

'*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.

Liam
  • 3
  • 2
  • I'm sorry I don't understand, that * works because it picks up 1.SNV. at the front of the file to run bgzip - please could you tell me more? – Liam Jan 16 '23 at 16:50
  • The problem is that the wildcard only expands if it finds a match. Since there's no file matching `*.111-T.vcf.gz`, the wildcard is not expanded. – Barmar Jan 16 '23 at 16:51
  • @LiamJohnson, It works if you don't quote `${vcf}` since there are no spaces and characters/string from the filename that are special to the shell. – Jetchisel Jan 16 '23 at 16:58

1 Answers1

0

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"
Barmar
  • 741,623
  • 53
  • 500
  • 612