简体   繁体   English

awk将所选行转置为列

[英]awk transpose selected rows to columns

Ive a text file in this format: 我使用这种格式的文本文件:

[Term]
id: HP:0000006
name: Autosomal dominant inheritance
alt_id: HP:0001415
alt_id: HP:0001447
alt_id: HP:0001448
alt_id: HP:0001451
alt_id: HP:0001455
alt_id: HP:0001456
alt_id: HP:0001463
def: "A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in heterozygotes. In the context of medical genetics, an autosomal dominant disorder is caused when a single copy of the mutant allele is present. Males and females are affected equally, and can both transmit the disorder with a risk of 50% for each child of inheriting the mutant allele." [HPO:curators]
synonym: "Autosomal dominant" EXACT []
synonym: "Autosomal dominant form" RELATED [HPO:skoehler]
synonym: "Autosomal dominant type" RELATED [HPO:skoehler]
xref: SNOMEDCT_US:263681008
xref: UMLS:C0443147
is_a: HP:0000005 ! Mode of inheritance

[Term]
id: HP:0000007
name: Autosomal recessive inheritance
alt_id: HP:0001416
alt_id: HP:0001526
def: "A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in homozygotes. In the context of medical genetics, autosomal recessive disorders manifest in homozygotes (with two copies of the mutant allele) or compound heterozygotes (whereby each copy of a gene has a distinct mutant allele)." [HPO:curators]
synonym: "Autosomal recessive" EXACT []
synonym: "Autosomal recessive form" RELATED [HPO:skoehler]
synonym: "Autosomal recessive predisposition" RELATED []
xref: SNOMEDCT_US:258211005
xref: UMLS:C0441748
xref: UMLS:C4020899
is_a: HP:0000005 ! Mode of inheritance

I would like to select and transpose 2 rows (first starts with 'name:' and second with 'def:' and is bounded by double quotes) from each group commencing with [Term] so that the following table be generated: 我想从[Term]开始的每个组中选择并转置2行(第一行以'name:'开头,第二行以'def:'开头并以双引号包围),以便生成下表:

column 1                         column 2
name                           | definition
Autosomal dominant inheritance | A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in heterozygotes. In the context of medical genetics, an autosomal dominant disorder is caused when a single copy of the mutant allele is present. Males and females are affected equally, and can both transmit the disorder with a risk of 50% for each child of inheriting the mutant allele.

Here is my attempt: 这是我的尝试:

gawk 'BEGIN{RS="[Term]"}{match($0, /^name:/, a) match($0, /^def:/, b) print a[1] , b[1]}' rows.txt > columns.txt

Awk solution: Awk解决方案:

awk 'BEGIN{ 
         printf "%-35s | definition\n","name" 
     }
     /^name:/{ sub(/^name: /, ""); name = $0 }
     /^def:/{ 
         gsub(/^def: "|"[^"]+$/, "");
         printf "%-35s | %s\n", name, $0 
     }' file

The output: 输出:

name                                | definition
Autosomal dominant inheritance      | A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in heterozygotes. In the context of medical genetics, an autosomal dominant disorder is caused when a single copy of the mutant allele is present. Males and females are affected equally, and can both transmit the disorder with a risk of 50% for each child of inheriting the mutant allele.
Autosomal recessive inheritance     | A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in homozygotes. In the context of medical genetics, autosomal recessive disorders manifest in homozygotes (with two copies of the mutant allele) or compound heterozygotes (whereby each copy of a gene has a distinct mutant allele).
$ cat tst.awk
BEGIN { OFS="\t| " }
{
    tag = val = $0
    sub(/:.*$/,"",tag)
    sub(/^[^:]+: *"?/,"",val)
    gsub(/".*$/,"",val)
    f[tag] = val
}
tag == "is_a" { print f["name"], f["def"] }

$ awk -f tst.awk file
Autosomal dominant inheritance  | A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in heterozygotes. In the context of medical genetics, an autosomal dominant disorder is caused when a single copy of the mutant allele is present. Males and females are affected equally, and can both transmit the disorder with a risk of 50% for each child of inheriting the mutant allele.
Autosomal recessive inheritance | A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in homozygotes. In the context of medical genetics, autosomal recessive disorders manifest in homozygotes (with two copies of the mutant allele) or compound heterozygotes (whereby each copy of a gene has a distinct mutant allele).

The above can easily be extended to print other values in the same tabular format: 上面的内容可以轻松扩展为以相同的表格格式打印其他值:

$ cat tst.awk
BEGIN { OFS="\t| " }
{
    tag = val = $0
    sub(/:.*$/,"",tag)
    sub(/^[^:]+: *"?/,"",val)
    gsub(/".*$/,"",val)
    f[tag] = val
}
tag == "is_a" { print f["name"], f["id"], f["is_a"], f["def"] }

$ awk -f tst.awk file
Autosomal dominant inheritance  | HP:0000006    | HP:0000005 ! Mode of inheritance  | A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in heterozygotes. In the context of medical genetics, an autosomal dominant disorder is caused when a single copy of the mutant allele is present. Males and females are affected equally, and can both transmit the disorder with a risk of 50% for each child of inheriting the mutant allele.
Autosomal recessive inheritance | HP:0000007    | HP:0000005 ! Mode of inheritance  | A mode of inheritance that is observed for traits related to a gene encoded on one of the autosomes (i.e., the human chromosomes 1-22) in which a trait manifests in homozygotes. In the context of medical genetics, autosomal recessive disorders manifest in homozygotes (with two copies of the mutant allele) or compound heterozygotes (whereby each copy of a gene has a distinct mutant allele).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM