unspecified field counts entire lines
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 4 Dec 2022 05:25:01 +0000 (06:25 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 12 Dec 2022 22:03:10 +0000 (23:03 +0100)
Expected behaviour equivalent to sort|uniq.

barcat
t/t1662-uniq_words.out
t/t1663-uniq_columns.out [new file with mode: 0644]
t/t1664-uniq_first_column.out
t/t1668-uniq_regex.out
t/t1806-reset_uniq.out

diff --git a/barcat b/barcat
index 0f0cfb8d100aefe0a12665c8ffbd493d605b0050..bae7989b29dff616924f8b105073f085007f7651 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -142,7 +142,6 @@ $opt{'graph-format'} //= '-';
 $opt{trim}   *= $opt{width} / 100 if $opt{trimpct};
 $opt{units}   = [split //, ' kMGTPEZYRQqryzafpn'.($opt{ascii} ? 'u' : 'μ').'m']
        if $opt{'human-readable'};
-$opt{anchor} //= qr/\A/;
 $opt{'value-length'} = 4 if $opt{units};
 $opt{'value-length'} = 1 if $opt{unmodified};
 $opt{'signal-stat'} //= exists $SIG{INFO} ? 'INFO' : 'QUIT';
@@ -211,13 +210,15 @@ if (defined $opt{interval}) {
 }
 
 my $float = qr<[0-9]* [.]? [0-9]+ (?: e[+-]?[0-9]+ )?>; # positive numberish
-my $valmatch = qr< $opt{anchor} ( \h* -? $float |) >;
+my $valmatch = $opt{anchor} // qr/\A/;
+$valmatch .= !$opt{count} ? qr/( \h* -? $float |)/ :
+       $opt{anchor} ? qr/(\S*)/ : qr/(.*)/;
+
 while (defined ($_ = $opt{input} ? shift @{ $opt{input} } : readline)) {
        s/\r?\n\z//;
        my $valnum;
        if ($opt{count}) {
-               ($valnum) = m/$opt{anchor} (\S*)/;
-               $valnum //= '';
+               $valnum = m/$valmatch/ && $1;
                $uniq{$valnum}++ and next;
                push @values, $valnum;
                s/\A/\n /;
@@ -535,9 +536,13 @@ or the I<NO_COLOR> environment variable.
 =item B<-c>, B<--count>
 
 Omit repetitions and count the number of occurrences.
-Similar to piping input to C<sort | uniq -c>
+Similar to piping input through C<sort | uniq -c>
 but keeping the order of first appearances.
 
+Lines are omitted if they (or a specified field) are identical,
+and the amount of matches is prepended and used as values
+for bars and subsequent statistics.
+
 =item B<-f>, B<--field>=([B<+>]I<number> | I<regexp>)
 
 Compare values after a given number of whitespace separators,
index 737a8851843f512c8d4b0a813d3f78566c55b0e1..2a5fff4727595f6675ae3e4677c2541d7a45a130 100644 (file)
@@ -1,7 +1,9 @@
 barcat --count input/duplicates.txt
- 3 thrice    ----<+->
- 2 twice     ----<
- 1 once      ---
-10 most      ----<+->=------------------
- 3           ----<+->
- 1 different ---
+ 3 thrice      -+-->=--
+ 2 twice       -+-->
+ 1 once        -+-
+10 most        -+-->=-------------------
+ 1             -+-
+ 1  indented   -+-
+ 1 different   -+-
+ 1   different -+-
diff --git a/t/t1663-uniq_columns.out b/t/t1663-uniq_columns.out
new file mode 100644 (file)
index 0000000..84e8306
--- /dev/null
@@ -0,0 +1,11 @@
+barcat -c input/duplicolumns.txt
+2 prefix 1 1 ------------+--=-----------
+1 random 2 2 ------------+-
+1 prefix 3 1 ------------+-
+1 text 1 2   ------------+-
+1 prefix 2 1 ------------+-
+2 prefix 3 2 ------------+--=-----------
+1 prefix 2 2 ------------+-
+1 prefix 3 0 ------------+-
+1 prefix 1 2 ------------+-
+1 prefix 2   ------------+-
index 31c1a161688cd003fda40db320ced0ce7bdcf3d6..a416c7641598d83579203c6385064f1776354286 100644 (file)
@@ -1,4 +1,4 @@
-barcat -c input/duplicolumns.txt
+barcat -c -f0 input/duplicolumns.txt
 10 prefix 1 1 -+-------=--->------------
  1 random 2 2 -+-
  1 text 1 2   -+-
index 7bbd521d7cfd232257708bde319d31b1ea33226b..6fc394c5690bdab95ae23e7dd4f1b4e6bb67a6d1 100644 (file)
@@ -1,11 +1,5 @@
-barcat -c -f'\S\K(.*)' input/duplicolumns.txt
-2 prefix 1 1 ------------+--=-----------
-1 random 2 2 ------------+-
-1 prefix 3 1 ------------+-
-1 text 1 2   ------------+-
-1 prefix 2 1 ------------+-
-2 prefix 3 2 ------------+--=-----------
-1 prefix 2 2 ------------+-
-1 prefix 3 0 ------------+-
-1 prefix 1 2 ------------+-
-1 prefix 2   ------------+-
+barcat -c -f'prefix\ ' input/duplicolumns.txt
+3 prefix 1 1 -------------------+
+2 random 2 2 --------------
+4 prefix 3 1 -------------------+-->----
+3 prefix 2 1 -------------------+
index 8b9467fdd5685aee949e8de18a26fb3185748b31..c245746cbc55d3c46dc9b55c85f10c3a2d3c8174 100644 (file)
@@ -6,6 +6,7 @@ barcat -ct-7 input/duplicates.txt
        -----            +++++++++++++++++
 6 most ----<--+---=>--------------------
 1      ----<-
-3 most      ---<----+=>---
-2  indented ---<----+
-1 different ---<-
+3 most        ---+---=->---
+1  indented   ---+
+1 different   ---+
+1   different ---+