Одно решение с использованием perl
, считая, что значения второго столбца отсортированы, я имею в виду, когда найденное значение 2
, все строки с одинаковым значением будут последовательными. Скрипт хранит строки до тех пор, пока не найдет другое значение во второй колонке, получить счетчик, распечатать их и освобождает память, поэтому не должен создавать проблемы независимо от того, насколько большой входной файл:
Содержание script.pl
:
use warnings;
use strict;
my (%lines, $count);
while (<>) {
## Remove last '\n'.
chomp;
## Split line in spaces.
my @f = split;
## Assume as malformed line if it hasn't four fields and omit it.
next unless @f == 4;
## Save lines in a hash until found a different value in second column.
## First line is special, because hash will always be empty.
## In last line avoid reading next one, otherwise I would lose lines
## saved in the hash.
## The hash will ony have one key at same time.
if (exists $lines{ $f[1] } or $. == 1) {
push @{ $lines{ $f[1] } }, $_;
++$count;
next if ! eof;
}
## At this point, the second field of the file has changed (or is last line), so
## I will print previous lines saved in the hash, remove then and begin saving
## lines with new value.
## The value of the second column will be the key of the hash, get it now.
my ($key) = keys %lines;
## Read each line of the hash and print it appending the repeated lines as
## last field.
while (@{ $lines{ $key } }) {
printf qq[%s\t%d\n], shift @{ $lines{ $key } }, $count;
}
## Clear hash.
%lines =();
## Add current line to hash, initialize counter and repeat all process
## until end of file.
push @{ $lines{ $f[1] } }, $_;
$count = 1;
}
Содержание infile
:
foobar1 1 xxx yyy
foobar1 2 xxx yyy
foobar2 2 xxx yyy
foobar2 3 xxx yyy
foobar1 3 xxx zzz
Выполнить это нравится:
perl script.pl infile
С следующим выводом:
foobar1 1 xxx yyy 1
foobar1 2 xxx yyy 2
foobar2 2 xxx yyy 2
foobar2 3 xxx yyy 2
foobar1 3 xxx zzz 2
пожалуйста вставить некоторые образцы данных, и ваш ожидает выход. – Kent