在bash中组合和多个文件并基于列数据进行组织

时间:2013-05-07 19:23:34

标签: bash scripting

我正在尝试将来自单个目录的115个文件组合在一起。这是一个 文件外观的示例:

FILE ONE

Phenotype    Marker    Value1    Value2    Value3
P1           1:54390   0.2948    0.4837    0.2198
P2           1:54390   0.3482    0.6583    0.1937
P3           1:54390   0.1983    0.1837    0.4177
P4           1:54390   0.9128    0.9930    0.0043
P5           1:54390   0.1938    0.0109    0.6573
P1           1:69402   0.2039    0.2340    0.2346
P2           1:69402   0.0239    0.3545    0.1987
P3           1:69402   0.8239    0.8677    0.4177
P4           1:69402   0.2498    0.3099    0.0765
P5           1:69402   0.0982    0.0198    0.0123

FILE TWO

Phenotype    Marker    Value1    Value2    Value3
P1           9:21048   0.8568    0.1231    0.1654
P2           9:21048   0.1244    0.3213    0.1223
P3           9:21048   0.9869    0.1231    0.4776
P4           9:21048   0.3543    0.7657    0.0033
P5           9:21048   0.1231    0.3213    0.8578
P1           9:87758   0.1231    0.8768    0.4653
P2           9:87758   0.7657    0.5435    0.8845
P3           9:87758   0.9879    0.8437    0.7464
P4           9:87758   0.1231    0.9879    0.5523
P5           9:87758   0.9879    0.9868    0.0006

所以基本上每个文件都有一组独特的标记,其中所有标记都是5 表型(P1,P2,P3,P4,P5)与它们匹配。

一些事情:

一个。我希望一个文件看起来像这样(下面),其中数据由组织    表型

Phenotype    Marker    Value1    Value2    Value3
P1           1:54390   0.2948    0.4837    0.2198
P1           1:69402   0.2039    0.2340    0.2346
P1           9:21048   0.8568    0.1231    0.1654
P1           9:87758   0.1231    0.8768    0.4653
P2           1:54390   0.3482    0.6583    0.1937
P2           1:69402   0.0239    0.3545    0.1987
P2           9:21048   0.1244    0.3213    0.1223     
P3           1:54390   0.1983    0.1837    0.4177
P3           1:69402   0.8239    0.8677    0.4177
P3           9:21048   0.9869    0.1231    0.4776
P3           9:87758   0.9879    0.8437    0.7464
P4           1:54390   0.9128    0.9930    0.0043
P4           1:69402   0.2498    0.3099    0.0765
P4           9:21048   0.3543    0.7657    0.0033
P4           9:87758   0.1231    0.9879    0.5523
P5           1:54390   0.1938    0.0109    0.6573
P5           1:69402   0.0982    0.0198    0.0123
P5           9:21048   0.1231    0.3213    0.8578
P5           9:87758   0.9879    0.9868    0.0006 

我想在 bash 中执行此操作。任何人都可以为我提供一些见解吗?我 这个语言很新!

B中。有了这个巨大的文件,我还想保存基于的单独文件 表型(我计划在中间做一些质量控制步骤),所以我 将有5个文件用于P1,P2,P3,P4和P5及其各自的数据 其他栏目)

4 个答案:

答案 0 :(得分:2)

#!awk -f
{
  /Phenotype/ ? hd=$0 : rw[$0]
}
END {
  print hd
  PROCINFO["sorted_in"] = "@ind_str_asc"
  for (each in rw) print each
}

答案 1 :(得分:2)

要解决A,你可以使用spiehr提出的方法。解决B:

# Name of your big merged file
BIG_FILE='...'


TYPES='P1 P2 P3 P4 P5'    
for T in $TYPES; do
    # Will reduce the input file to
    # all lines starting with $T, which is one of P1, P2 etc.,
    # and write them to a file named accordingly
    grep "^$T" $BIG_FILE > file_$T
done

答案 2 :(得分:0)

获取标题,列标题为:

head -1 > tmpfile

可以像这样插入数据:

for file in $(ls); do
    tail -n +2 ${file} >> tmpfile2
done
sort tmpfile2 >> tmpfile
rm tmpfile2

tmpfile将是包含所有数据的文件。 而不是写$(ls)你可以添加另一个linux命令,列出所有相关文件。

要获取第一列中只有'P3'的条目,您可以使用grep:

grep '^P3' tmpfile | cut -f1 --complement

剪切命令用于剪切第一个条目,您可能不再需要它了。

答案 3 :(得分:0)

我将第一步写为

{
    sed 1q file1
    sed 1d * | sort
} > file_all

然后

awk '
    FNR == 1 {head = $0; next}
    !seen[$1]++ {print head > $1}
    {print > $1}
' file_all

这导致名为“P1”,“P2”等的文件

相关问题