gpt4 book ai didi

file - awk 将单个文件分成具有特定文件名的多个文件

转载 作者:行者123 更新时间:2023-12-01 22:12:46 24 4
gpt4 key购买 nike

我有一个原始文件,其中包含以下特定格式的数据:

$ cat sample.txt
>MA0002.1 RUNX1
A [ 10 12 4 1 2 2 0 0 0 8 13 ]
C [ 2 2 7 1 0 8 0 0 1 2 2 ]
G [ 3 1 1 0 23 0 26 26 0 0 4 ]
T [ 11 11 14 24 1 16 0 0 25 16 7 ]
>MA0003.1 TFAP2A
A [ 0 0 0 22 19 55 53 19 9 ]
C [ 0 185 185 71 57 44 30 16 78 ]
G [ 185 0 0 46 61 67 91 137 79 ]
T [ 0 0 0 46 48 19 11 13 19 ]
>MA0003.3 TFAP2C
A [ 1706 137 0 0 33 575 3640 1012 0 31 1865 ]
C [ 1939 968 5309 5309 1646 2682 995 224 31 4726 798 ]
G [ 277 4340 139 11 658 1613 618 5309 5309 582 1295 ]
T [ 1386 47 0 281 2972 438 56 0 0 21 1350 ]

我想根据字母>将这个文件分成单独的文件我知道这个字符出现在每 5 行之后。我可以通过以下方式做到这一点:

awk 'NR%5==1{x="F"++i;}{print > x}' sample.txt

问题是它正确创建了多个文件,但文件名分别为 F1、F2 和 F3,并且没有任何扩展名。我想用第一行中提到的名称保存这个单独的文件,即 RUNX1 , TFAP2ATFAP2C并带有 .pfm 的扩展名.

这样最终的文件看起来像:

$ cat RUNX1.pfm
>MA0002.1 RUNX1
A [ 10 12 4 1 2 2 0 0 0 8 13 ]
C [ 2 2 7 1 0 8 0 0 1 2 2 ]
G [ 3 1 1 0 23 0 26 26 0 0 4 ]
T [ 11 11 14 24 1 16 0 0 25 16 7 ]

$ cat TFAP2A.pfm
>MA0003.1 TFAP2A
A [ 0 0 0 22 19 55 53 19 9 ]
C [ 0 185 185 71 57 44 30 16 78 ]
G [ 185 0 0 46 61 67 91 137 79 ]
T [ 0 0 0 46 48 19 11 13 19 ]

等等..

感谢您抽出时间帮助我!

最佳答案

以下 awk 可能会对您有所帮助。

awk '/^>/{if(file){close(file)};file=$2".pfm"} {print > file".pfm"}'  Input_file

在这里也添加了一个带有解释的非单行表格。

awk '
/^>/{ ##Checking here if any line starts with ">" if yes then do following actions.
if(file){ ##Checking if value of variable named file is NOT NULL, if condition is TRUE then do following.
close(file) ##close is awk out of the box command which will close any opened file, so that we could avoid situation of too many files opened at a time.
};
file=$2".pfm" ##Setting variable named file to 2nd filed of the line which starts from ">" here.
}
{
print > file".pfm"##Printing the value of current line to file".pfm" which will create file with $2 and .pfm name and put output into output files.
}
' Input_file ##Mentioning the Input_file name here.

编辑:

awk '/^>/{if(file){close(file)};array[$2]++;file=array[$2]?(array[$2]==1?$2:$2"."array[$2]):$2} {print > file".pfm"}'  Input_file

关于file - awk 将单个文件分成具有特定文件名的多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46957444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com