在循环中连接文件对

在循环中连接文件对

我有两个以两个不同扩展名结尾的文件列表,我想在循环中将它们成对连接。文件名如下所示:

这些是文件

a.ID, b.ID, c.ID, d.ID     
a.value, b.value, c.value, d.value  

直觉上我会这样做:

for i in *.ID;  
do  
     for j in *.value;  
do  
     cat $i $j > $i.txt ; done  
done  

问题是我想a.IDa.valueb.ID合并b.value,这样它们就会随机合并。就像a.valueb.ID等..

任何想法?提前致谢

输入样本a.ID(例如):

chr1_237301_237601  176 1  
chr1_237601_237901  176 1  
chr1_237901_238201  176 1 

示例结尾为a.value(例如):

chr1_1_301      0   0  
chr1_301_601    0   0  
chr1_601_901    0   0  
chr1_901_1201   0   0  
chr1_1201_1501  0   0  

输出:

chr1_237301_237601  176 1    
chr1_237601_237901  176 1   
chr1_237901_238201  176 1  
chr1_1_301      0   0    
chr1_301_601    0   0   
chr1_601_901    0   0  
chr1_901_1201   0   0  
chr1_1201_1501  0   0

答案1

你不需要两个循环。您需要一个循环,通过a、等bc像这样:

for i in *.ID; do
    b=${i%%.ID}
    cat "$i" "$b".value >"$b".txt
done

答案2

正因为可以这样做,所以这里有find+bash方法。

$ ls
a.ID  a.value

$ find -type f -iname "*.ID" -exec bash -c 'base=$(basename -s ".ID" "$@");cat "$@" "$base".value > "$base".merged' sh   "{}" \;

$ ls
a.ID  a.merged  a.value

$ cat a.merged                                                                                                           
chr1_237301_237601  176 1  
chr1_237601_237901  176 1  
chr1_237901_238201  176 1 
chr1_1_301      0   0  
chr1_301_601    0   0  
chr1_601_901    0   0 

相关内容