Loop to concatenate multiple pairs of files with almost the same name in UNIX

给你一囗甜甜゛ 提交于 2019-12-11 10:08:59

问题


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.fastq Sample1_R2_L001.fastq Sample2_R1_L001.fastq Sample2_R2_L001.fastq Sample3_R1_L001.fastq Sample3_R2_L001.fastq

(etc...)

The result I want is to concatenate by sample, such as cat Sample1_R1_L001.fastq Sample1_R2_L001.fastq > Sample1_concat.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?


回答1:


Here's how I'd do it:

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

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.

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



来源:https://stackoverflow.com/questions/25019344/loop-to-concatenate-multiple-pairs-of-files-with-almost-the-same-name-in-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!