如何从多个文件中提取特定信息并在linux中创建表?

时间:2018-05-27 12:19:59

标签: linux grep text-files tabular text-extraction

我有多个包含信息的文本文件。这里我展示了两个文本文件,如下所示:

Sample1.txt

Status  /documents/Sample1.sorted.bam
Assigned        50945040
Unassigned_Unmapped     947866
Unassigned_MappingQuality       0
Unassigned_Chimera      0
Unassigned_FragmentLength       0
Unassigned_Duplicate    0
Unassigned_MultiMapping 49013681
Unassigned_Secondary    0
Unassigned_Nonjunction  0
Unassigned_NoFeatures   21189312
Unassigned_Overlapping_Length   0
Unassigned_Ambiguity    4430011

Sample2.txt

Status  /documents/Sample2.sorted.bam
Assigned        36335614
Unassigned_Unmapped     870456
Unassigned_MappingQuality       0
Unassigned_Chimera      0
Unassigned_FragmentLength       0
Unassigned_Duplicate    0
Unassigned_MultiMapping 68688141
Unassigned_Secondary    0
Unassigned_Nonjunction  0
Unassigned_NoFeatures   23746485
Unassigned_Overlapping_Length   0
Unassigned_Ambiguity    3734593

对于单文本文件,我使用的是grep:

grep "Assigned\|Unmapped\|MultiMapping\|NoFeatures\|Ambiguity" Sample1.txt > output.txt

但我希望输出如下所示我可以在所有文本文件上使用小脚本并制作表格:

                        Sample1       Sample2
Assigned                50945040      36335614
Unassigned_Unmapped     947866        870456
Unassigned_MultiMapping 49013681      68688141
Unassigned_NoFeatures   21189312      23746485
Unassigned_Ambiguity    4430011       3734593

1 个答案:

答案 0 :(得分:2)

/usr/lib/xymon/server/bin/xymon

要获得Excel可以理解的输出而不是问题中显示的输出,请执行以下操作:

$ cat tst.awk
$2 != 0 {
    printf "%s%s", (NR>1 ? $1 : "Name"), OFS
    for (i=2; i<=NF; i+=2) {
        gsub(/^.*\/|\..*$/,"",$i)
        printf "%s%s", $i, (i<NF ? OFS : ORS)
    }
}

$ paste Sample1.txt Sample2.txt | awk -f tst.awk | column -t
Name                     Sample1   Sample2
Assigned                 50945040  36335614
Unassigned_Unmapped      947866    870456
Unassigned_MultiMapping  49013681  68688141
Unassigned_NoFeatures    21189312  23746485
Unassigned_Ambiguity     4430011   3734593

然后双击output.csv以使用Excel打开它。