awk транспонирует выбранные строки в столбцы

Имею текстовый файл в таком формате:

[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

Я хотел бы выбрать и перенести 2 строки (первая начинается с «name:», а вторая с «def:» и ограничена двойными кавычками) из каждой группы, начиная с [Term], чтобы была создана следующая таблица:

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.

Вот моя попытка:

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

awk
person gungu    schedule 02.04.2018    source источник
comment
Я думал что-то вроде 'BEGIN{RS=[Term]}{match($0, /^name:/, a) match($0, /^def:/, b) print a[1], b[1] }' файл_rows.txt › файл_columns.txt   -  person gungu    schedule 02.04.2018


Ответы (2)


$ 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).

Приведенное выше можно легко расширить для печати других значений в том же табличном формате:

$ 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).
person Ed Morton    schedule 02.04.2018

Awk решение:

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

Выход:

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).
person RomanPerekhrest    schedule 02.04.2018
comment
Спасибо, Роман. Если бы я хотел заменить 35 пробелов табуляцией, как бы я это сделал? - person gungu; 02.04.2018