![](/img/trans.png)
[英]split text file (Genome data) based on column values keeping header line
[英]Spliting a text file's contents based on repetitive line values
我有一個單列文本文件,例如:
A.txt
0;
1;
2;
3;
.
.
.
0;
4;
8;
.
.
.
0;
6;
9;
目標是根據行的值將A.txt拆分為文件,這樣對於在A.txt中多次出現的每個行值,都必須基於該值進行單獨的拆分。 這是假設“ 0;”的所需輸出文件的示例。 是A.txt中唯一的重復元素:
A1.txt
0;
1;
2;
3;
.
.
.
A2.txt
0;
4;
8;
.
.
.
A3.txt
0;
6;
9;
.
.
.
任何想法如何通過linux bash腳本實現?
Perl解救:
#!/usr/bin/perl
use warnings;
use strict;
my @lines = <>;
chomp @lines;
my %count;
$count{$_}++ for @lines;
my $OUT;
my $x;
for my $separator (grep $count{$_} > 1, keys %count) {
for my $line (@lines) {
open $OUT, '>', 'A' . ++$x . '.txt' or die $!
if not $OUT or $separator eq $line;
print {$OUT} "$line\n";
}
undef $OUT;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.