Home > OS >  Bash: Identifying file based on part of filename
Bash: Identifying file based on part of filename

Time:03-04

I have a folder containing paired files with names that look like this:

PB3999_Tail_XYZ_1234.bam
PB3999_PB_YWZ_5524.bam

I want to pass the files into a for loop as such:

for input in `ls PB*_Tail_.bam`; do tumor=${input%_Tail_*.bam}; $gatk Mutect2 -I $input -I$tumor${*}; done

The issue is, I can't seem to get the syntax right for the tumor input. I want it to recognise the paired file by the first part of the name PB3999_PB while ignoring the second half of the file name _YWZ_5524 that does not match.

Thank you for any help!

CodePudding user response:

Just replaced ${*} with * and added _PB_ suffix to the prefix, to the script in the question. And, renamed variables.

for tailfname in PB*_Tail_*.bam; do
    pairprefix="${tailfname%_Tail_*.bam}"
    echo command with ${tailfname} ${pairprefix}_PB_*.bam
done

Hope this helps. The name tumor sounds scary. Hope the right files are paired.

CodePudding user response:

I'm trying to fully understand what you want to do here.

If you want to extract just the first two parts, this should do:

echo "PB3999_Tail_XYZ_1234.bam" | cut -d '_' -f 1-2

That returns just the "PB3999_Tail" part.

  •  Tags:  
  • bash
  • Related