Vince
Vince

Reputation: 245

How do I sort and use multiple files in a bash script?

I have multiple pairs of files that differ only in one number:

121_S11_L001_R1_001

121_S11_L001_R2_001

and another pair relative to other files, differ in multiple numbers, but relative to its pair, again only in one number:

120_S10_L001_R1_001

120_S10_L001_R2_001

I have a bash script to process these files individually:

if [ -s $infile ] && [ ! -s $infile.bwa ]; then

echo "Creating BWA file..."
time bwa aln $path"Genomeidx" $infile > $infile.bwa
time bwa aln $path"Genomeidx" $infile2 > $infile2.bwa

Where 'infile' and 'infile2' are files specified on the command line

Instead of manually typing each pair, how do I recursively select and process each file for each pair?

Upvotes: 0

Views: 244

Answers (1)

Austin Phillips
Austin Phillips

Reputation: 15746

You can use bash globbing to pick out all the _R1_ style files and then locate its pair.

Something like this could be adapted to do your processing:

#!/bin/bash

for file in *_R1_*; do
    pair=${file/_R1_/_R2_}
    if [ -f "$pair" ]; then
        echo "processing $file and its pair $pair"
    fi
done

Upvotes: 2

Related Questions