在不相关的数据条目之间添加一个空行 [英] Adding a blank line between unrelated data entries
问题描述
我有一个数据文件,我需要将它用于程序的输入,但我需要稍微调整格式.使用这种方法:从文件中提取特定数据并将其写入另一个文件 我生成了一个如下所示的文件:
I have a data file that I need to use an input for a program, but I need to tweak the formatting a little. Using this method: Extracting specific data from a file and writing it to another file I generated a file that looks like this:
PITG_00002 2 397
PITG_00004 1 1275
PITG_00004 1397 1969
PITG_00005 200 1111
PITG_00005 1281 1646
PITG_00006 1 816
PITG_00009 2398 3276
PITG_00009 1536 1952
PITG_00010 1 537
我需要区分来自相同序列(第一列)的数据和来自不同序列的数据,方法是在唯一的序列之间添加一个空行,使其看起来像:
I need to distinguish between data that comes from the same sequence (first column) and data that comes from different sequences, by adding a blank line in between sequences that are unique, so that it looks like:
PITG_00002 2 397
PITG_00004 1 1275
PITG_00004 1397 1969
PITG_00005 200 1111
PITG_00005 1281 1646
PITG_00006 1 816
PITG_00009 2398 3276
PITG_00009 1536 1952
PITG_00010 1 537
我用我可用的程序/编码选项标记了它.非常感谢您能提供的任何帮助,谢谢!
I tagged this with the program/coding options available to me. Any help you could give is much appreciated, thanks!
推荐答案
perl -pae 'print $/ if (defined $x && $x ne $F[0]); $x = $F[0];' input.txt
这将根据存储在 $x
中的前一个字段检查第一个字段 $F[0]
.如果它们不相同,则打印换行符.
This will check the first field $F[0]
against the previous field, stored in $x
. If they are not the same, a newline is printed.
说明:
-p
读取文件并打印每一行-a
将空白行自动拆分为@F
数组$/
是您的输入记录分隔符,默认为换行符.
-p
read file and print each line-a
autosplit lines on whitespace into@F
array$/
is your input record separator, default is newline.
这篇关于在不相关的数据条目之间添加一个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!