【发布时间】:2020-07-05 00:17:10
【问题描述】:
我有一个看起来像这样的file.txt(我删除了行以简化我的示例):
PLXNA3 ### <- filename1
Missense/nonsense : 13 mutations # <- header spaces
accession codon_change amino_acid_change # <- column names tsv
ID73 CAT-TAT His66Tyr # <- line tsv
ID63 GAC-AAC Asp127Asn # <- line tsv
ID31 GCC-GTC Ala307Val # <- line tsv
NEDD4L ### <- filename2
Splicing : 1 mutation # <- header spaces
accession splicing_mutation # <- column names tsv
ID51 IVS1 as G-A -16229 # <- line tsv
Gross deletions : 1 mutation # <- header spaces
accession DNA_level description HGVS_(nucleotide) HGVS_(protein) # <- column names tsv
ID853 gDNA 4.5 Mb incl. entire gene Not yet available Not yet available # <- line tsv
OPHN1 ### <- filename3
Small insertions : 3 mutations # <- header spaces
accession insertion HGVS_(nucleotide) # <- column names tsv
ID96 TTATGTT(^183)TATtCAAATCCAGG c.549dupT p.(Gln184Serfs*23) # <- line tsv
ID25 GTGCT(^310)AAGCAcaG_EI_GTCAGTTCT c.931_932dupCA # <- line tsv
我想拆分这个文件以获得3个不同的文件:
PLXNA3.txt
PLXNA3 ### <- filename1
Missense/nonsense : 13 mutations # <- header spaces
accession codon_change amino_acid_change # <- column names tsv
ID73 CAT-TAT His66Tyr # <- line tsv
ID63 GAC-AAC Asp127Asn # <- line tsv
ID31 GCC-GTC Ala307Val # <- line tsv
NEDD4L.txt
NEDD4L ### <- filename2
Splicing : 1 mutation # <- header spaces
accession splicing_mutation # <- column names tsv
ID51 IVS1 as G-A -16229 # <- line tsv
Gross deletions : 1 mutation # <- header spaces
accession DNA_level description HGVS_(nucleotide) HGVS_(protein) # <- column names tsv
ID853 gDNA 4.5 Mb incl. entire gene Not yet available Not yet available # <- line tsv
OPHN1
OPHN1 ### <- filename3
Small insertions : 3 mutations # <- header spaces
accession insertion HGVS_(nucleotide) # <- column names tsv
ID96 TTATGTT(^183)TATtCAAATCCAGG c.549dupT p.(Gln184Serfs*23) # <- line tsv
ID25 GTGCT(^310)AAGCAcaG_EI_GTCAGTTCT c.931_932dupCA # <- line tsv
如何使用任何 linux 命令(如 awk 或 python)来实现所需的输出?
注意:
- 文件名没有任何空格或制表符,但它们可能包含
-。 - 标题包含空格。
- 行是制表符分隔的。
- 真正的分隔符应该是文件名,因为我可以有多个标题。
提前致谢。
【问题讨论】:
-
是否保证除了文件名行之外的所有行都包含空格或制表符?
-
@slin 是的,这是有保证的。
-
是否给定文件名,例如
NEDD4L,曾经在输入中重复还是只出现一次? -
@EdMorton 它只出现一次,因为它是一个基因名称。
标签: python regex file awk split