簡體   English   中英

根據重復的行值拆分文本文件的內容

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM