bash - How to match files names to use in loop

2020-05-02 15:48发布

问题:

I want to run a loop command on each pair of files

a_xxx.wig
b_xxx.wig
c_xxx.wig

a_xxx.bed
b_xxx.bed
c_xxx.bed 

I'd like to match the files based on sample name (a,b or c) and have the loop run a command through each of the pairs without having to do one at a time

for file in * do
bedtools intersect -a $sample.wig -b $sample > $sample.bed
done

Any ideas or pointers would be greatly appreciated

回答1:

The wig file is the input, so just iterate over those. You can extract the portion of the file name to use as the "base" for the other names.

for file in *.wig; do
    sample=${file%.wig}
    bedtools intersect -a "$sample.wig" -b "$sample" > "$sample.bed"
done


标签: linux bash