Question

what command with perl would allow me to count lines? in the output file should be at the start of the line how many times the contiguous lines appeared in a previous sorted file used as input.

input example :

line 1
line 2
line 2
line 2
line 3
line 3
line 3

ouput:

1= line 1
3= line 2
3= line 3
Was it helpful?

Solution

In a perl one-liner:

perl -ne '$c{$_}++; END {print "$c{$_}= $_" for keys %c}' your_file

If you'd like to stream the results for a sorted file so that it stays in order and isn't loaded entirely into memory:

perl -ne 'if (defined $l && $l ne $_) {print "$c= $l"; $c=0} $l=$_;$c++; END{print "$c= $l"}' your_file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top