'Bash: Identifying file based on part of filename

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!



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Juan J. Cuéllar