简体   繁体   中英

Bash script to loop through similar files and input into a command

I'm a newbie to bash scripting and currently trying to write a script but failed. I have a list of paired samples that need to be fed into a script that will merge the 2 files into 1 file. The script for merging 2 files is:

./mergePEsam.pl file1_1.sam file1_2.sam file.merge.sam

D3_524_1_tr.fq.gz.sam
D3_524_2_tr.fq.gz.sam
D3_525_1_tr.fq.gz.sam
D3_525_2_tr.fq.gz.sam
D3_526_1_tr.fq.gz.sam
D3_526_2_tr.fq.gz.sam
WT_541_1_tr.fq.gz.sam
WT_541_2_tr.fq.gz.sam
WT_542_1_tr.fq.gz.sam
WT_542_2_tr.fq.gz.sam

I'm trying to create a loop that will go through and merge the sample_1.sam and sample_2.sam using the script above. The script I've written so far that fails is:

for r1 in "~scratch/leafcutter/olego.alignment"/*1_tr.fq.gz.sam; do
        r2=${r1%_tr.fq.gz.sam}_tr.fq.gz.sam
        if [[ -f $r2 ]] ; then
                users/djaime/anaconda/leafcutter3/olego/mergePEsam.pl "$r1" "$r2" "${r1/_*/}".merge.sam
        else
                echo "$r2 not found" >&2
        fi
done

Help would be much appreciate

In the current script, r2 is set to the same name as r1. Looks from description, it should be set to the _2 file. Consider minor fix that will replace the _1 with _2:

r2=${r1%1_tr.fq.gz.sam}2_tr.fq.gz.sam

How do you want to merge two files? If you want to just concatenate file1_2.sam after file1_1.sam,

cat file1_1.sam file1_2.sam > file.merge.sam

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM