smlr

smlr

Cargo install

    cargo install smlr

This is the documentation for smlr v0.1.2. The rustdoc can be found here.

Intro

smlr is a command line utility that helps you find similar entries in text data. It is doe not aim at replacing tools such as uniq or awk.

The following data is available in the samples folder. Let’s consider a few examples:

Performance

A test run on my iMac (fusion drive, no SSD) shows 11MB processed under 30s.

   $ TARGET=/var/log/install.log; du -h $TARGET | head -n 1; time smlr -lcb $TARGET > /tmp/output.txt
   11M    /var/log/install.log
   Finished release [optimized] target(s) in 0.01s
   Running `target/release/smlr -lcb /var/log/install.log`

   real    0m19.603s
   user    0m18.310s
   sys     0m1.202s

Example #1

Say we want to find duplicates in the following data:

Pizza
Ice Cream
Waffle
PiZZa
Waffle
pizza
Pizza
Peanuts
PIZZA
Beef Jerky
Popcorn
pizza
pizza

To my surprise, uniq seems to fail at find case insensitive matches:

 uniq -i -c file03.txt
   1 Pizza
   1 Ice Cream
   1 Waffle
   1 PiZZa
   1 Waffle
   2 pizza
   1 Peanuts
   1 PIZZA
   1 Beef Jerky
   1 Popcorn
   2 pizza

The result from uniq is a bit suprising as I expected to find 4x pizza and not 2 here and 2 there. This issue can be easily solved using the sort command.

In the following example, we process the input using sort to group all duplicates. We then use sort again to show the most duplicates first.

$ cat file03.txt | sort | uniq -i -c | sort -r
   3 pizza
   3 PiZZa
   2 Waffle
   1 Popcorn
   1 Peanuts
   1 PIZZA
   1 Ice Cream
   1 Beef Jerky

Still uniq has issue with duplicates with different case despite using -i but sort can help further.

$ cat file03.txt | sort -f | uniq -i -c | sort -r
   7 PIZZA
   2 Waffle
   1 Popcorn
   1 Peanuts
   1 Ice Cream
   1 Beef Jerky

Example #2

If we introduce some variance on some fields in our input, uniq will likely fail on finding the duplicates.

Here is the data we work on:

2020-01-01  Pizza
2020-01-01  Ice Cream
2020-01-02  Waffle
2020-01-03  PiZZa
2020-03-05  Waffle
2020-03-05  pizza
2020-03-06  pizza
2020-03-06  Peanuts
2020-03-06  PIZZA
2020-03-06  Beef Jerky
2020-03-07  Popcorn
2020-03-07  pizza
2020-03-08  pizza

Here we are stuck with out previous sort+uniq method. None of the following really work as expected: - cat file04.txt | sort -f | uniq -i -c -f 1 - cat file04.txt | sort -f | uniq -i -c -s 10

The reason for the failure is that our sort trick no longer works. We call awk to the rescue:

$ cat file04.txt | awk '{print $2}' | sort -f | uniq -c -i
   1 Beef
   1 Ice
   1 Peanuts
   7 PIZZA
   1 Popcorn
   2 Waffle

Example #3

Here is a nasty example where uniq, even with helps of some other friendly commands, won’t be able to be helpful:

2020-01-01  Pizza
2020-01-01  Ice Cream
2020-01-01  Ice  Cream
2020-01-02  Waffle
2020-01-03  PiZZa
2020-03-05  Waflle
2020-03-05  pizza
2020-03-06  piiza
2020-03-06  Peanuts
2020-03-06  PlZZA
2020-03-06  Beef Jerky
2020-03-07  POPCORN
2020-03-07  P0PCORN
2020-03-07  pizza
2020-03-08  pizza

This example contains a few typos on purpose. Those are the typos typically hard to spot (depending on your font and concentration level!).

Let see how smlr can handle that.

While smlr can do more, it has a cost in CPU and memory. Beware when parsing huge files!

Install from GIT

    cargo install --git https://gitlab.com/chevdor/smlr.git --tag v0.1.2

Installing the version from `master` is not recommended for production: do *NOT* omit the `--tag v0.1.2` in the previous command.

License

MIT License

Copyright (c) 2019-2020 Wilfried Kopp - Chevdor

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.