使用变量名作为cat的输出

时间:2015-08-13 16:49:46

标签: bash

我在这里遇到了一些来自bash的奇怪行为。我有几个文件,其中一些是多个部分。每个名为 * _ Rx_00y.fastq.gz 的文件都应与 x 连接作为标识符,即R1_001和R1_002(以及假设的R1_003)一起使用。

    var appMainModule = angular.module('appMain', []);

appMainModule.controller("homePageViewModel", function($scope, $http,     $location){ 
    $scope.Heading = "This is the heading";

    $scope.SayHello = function () {
        alert('Hello');
    }
});

我希望同时获取* _R1_00x.fastq.gz文件并将它们连接到第一个文件。我意识到我可以在这里使用 [mark@theNosebook Sample_P4]$ ls -lh total 822M -rwxr-xr-x 1 mark mark 404M Aug 13 12:25 P4_CTCTCTAC-AGAGTAGA_L002_R1_001.fastq.gz -rwxr-xr-x 1 mark mark 2.6M Aug 13 12:25 P4_CTCTCTAC-AGAGTAGA_L002_R1_002.fastq.gz -rwxr-xr-x 1 mark mark 414M Aug 13 12:25 P4_CTCTCTAC-AGAGTAGA_L002_R2_001.fastq.gz -rwxr-xr-x 1 mark mark 2.6M Aug 13 12:25 P4_CTCTCTAC-AGAGTAGA_L002_R2_002.fastq.gz -rwxr-xr-x 1 mark mark 144 Aug 13 12:25 SampleSheet.csv ,但如果我有超过2个条目,它似乎很笨拙。我认为 工作的解决方案是:

>>

但是,我来了

    name=$(ls *_R1_001.fastq.gz)
    cat $(ls *_R1_*) > ${name}

请注意,结果输出的大小仅为第二个文件(2.6M)的大小。将它们写入单独的文件,这里 cat ,工作正常。

[mark@theNosebook Sample_P4]$ ls -lh
total 421M
-rwxr-xr-x 1 mark mark 2.6M Aug 13 12:37 P4_CTCTCTAC-AGAGTAGA_L002_R1_001.fastq.gz
-rwxr-xr-x 1 mark mark 2.6M Aug 13 12:25 P4_CTCTCTAC-AGAGTAGA_L002_R1_002.fastq.gz
-rwxr-xr-x 1 mark mark 414M Aug 13 12:25 P4_CTCTCTAC-AGAGTAGA_L002_R2_001.fastq.gz
-rwxr-xr-x 1 mark mark 2.6M Aug 13 12:25 P4_CTCTCTAC-AGAGTAGA_L002_R2_002.fastq.gz
-rwxr-xr-x 1 mark mark  144 Aug 13 12:25 SampleSheet.csv

这里发生了什么?我想保留文件名,因为它们引用了从中取样的样本。

由于

3 个答案:

答案 0 :(得分:1)

您不需要使用ls;您使用ls的任何模式都可以用于填充数组,然后可以将其内容用作cat的参数。首先将所有内容写入临时文件,以确保在覆盖第一个文件之前连接成功。

to_cat=( *_R1_* )
tmp=$(mktemp)
cat "${to_cat[@]}" > "$tmp" && mv "$tmp" "${to_cat[0]}"

您可以选择确保找到要连接的文件。 (我推荐它,只是为了安全。)

shopt -s nullglob
to_cat=( *_R1_*)
tmp=$(mktemp)
(( ${#to_cat[@]} )) && cat "${to_cat[@]}" > "$tmp" && mv "$tmp" "${to_cat[0]}"

答案 1 :(得分:1)

由于你想保留文件名,我收集文件名中的所有直到最后一个下划线是保留的候选者,最后三位数字是递增的块标识符。

因此,您可能希望处理大量这些文件,而不必为每个文件修改脚本。

这个怎么样?

#!/usr/bin/env bash

# Detect a "-f" option, which forces recreation of files.
if [ "$1" = "-f" ]; then
  force=true
else
  force=false
fi

# First, get our list of prefixes into an array,
# stripping from the last underscore to the end of each name.
a=(*.fastq.gz)
prefixes="${a[@]%_*}"

# Next, step through the prefixes array, concatenating the chunks.
for prefix in "${prefixes[@]}"; do
  if [ ! -s "${prefix}_joined.fastq.gz" ] || $force; then
    cat "${prefix}"_[0-9]*.fastq.gz > "${prefix}_joined.fastq.gz"
  fi
done

注意" -f"选项。我已经将它包含在内,这样如果你 在大量文件上运行它,脚本将快速跳过在上一批中处理过的文件。

我建议您在单独的文件中加入您的文件,而不是覆盖您的第一个文件,这样如果出现问题,您就不会损坏您的原始数据。毕竟,结果应该是可重复的! : - )

答案 2 :(得分:-1)

您必须先gunzip

尝试:

gunzip -c *_R1_001.fastq.gz | gzip > result.gz