本文介绍了在UNIX中循环连接多对名称几乎相同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的问题,但我无法解决.我在同一目录中有多个文件,我想串联每对文件.名称是:

I have a very basic question, but I can't get a solution. I have multiple files in the same directory and I would like to concatenate each pair of files. The names are:

Sample1_R1_L001.fastqSample1_R2_L001.fastqSample2_R1_L001.fastqSample2_R2_L001.fastqSample3_R1_L001.fastqSample3_R2_L001.fastq

Sample1_R1_L001.fastqSample1_R2_L001.fastqSample2_R1_L001.fastqSample2_R2_L001.fastqSample3_R1_L001.fastqSample3_R2_L001.fastq

(等...)

我想要的结果是按样本连接,例如cat Sample1_R1_L001.fastq Sample1_R2_L001.fastq> Sample1_concat.fastq

The result I want is to concatenate by sample, such ascat Sample1_R1_L001.fastq Sample1_R2_L001.fastq > Sample1_concat.fastq

我尝试了这个循环,找 . -名称" _R?_ " |读取文件时;做"$ file" R1 *.fastq"$ file" _R2_L001.fastq>"$ file" _merged.fastq

I tried this loop,find . -name "_R?_"|while read file; do "$file"R1*.fastq "$file"_R2_L001.fastq > "$file"_merged.fastq

但是没有用.有什么想法吗?

but it didn't work. Any thoughts?

推荐答案

这是我的处理方式:

for i in {1..4}; do echo cat Sample"$i"* > combined_"$i".txt; done

遍历数字然后构建命令...确实可以获取实际的文件名,但是使用shell,我只是做了可能可行的最简单的事情,即使这意味着部分地强制执行循环.

Loop over the numbers then build the command... getting the actual filenames is surely possible, but with shell, I just do the simplest thing that can possibly work, even if that means partially brute forcing the loop.

将1..4替换为任何限制,shell会将其扩展为适合您的所有不同数字.

Replace the 1..4 to whatever the limits are and the shell will expand that into all the various numbers for you.

这篇关于在UNIX中循环连接多对名称几乎相同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 16:40