smlr
This is the documentation for smlr v0.2.9
.
The rustdoc can be found here.
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:
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
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
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!
cargo install --git https://gitlab.com/chevdor/substrate-runtime-hasher.git --tag v0.2.9
Installing the version from `master` is not recommended for production: do *NOT* omit the `--tag v0.2.9` in the previous command.
In the following examples, the path of the WASM file will be stored into a variables called WASM
.
You may set it with:
export WASM=/home/foobar/kusama/target/srtool/release/wbuild/kusama-runtime/kusama_runtime.compact.wasm
Build your runtime… or let {srtool} do it.
cd <polkadot-repo>
# build the runtime (you could use srtool)
Invoke smlr
.
smlr $WASM
0x5931690e71e9d3d9f04a43d8c15e45e0968e563858dd87ad6485b2368a286a8f
If you are using {srtool}, it will do everything for you. The default location for the WASM will be inside your repo in $WASM
(for instance, for kusama).
Since v0.2.1
, you can also use Unix pipes:
Piping into smlr.
cat $WASM | smlr
0x5931690e71e9d3d9f04a43d8c15e45e0968e563858dd87ad6485b2368a286a8f
Input redirection.
smlr < $WASM
0x5931690e71e9d3d9f04a43d8c15e45e0968e563858dd87ad6485b2368a286a8f
If you are using commands such as echo
make sure to NOT send an extra newline
. For instance echo "123" | smlr
will not hash 123
but 123<cr>
. In that case, you probably want to use echo -n "123" | smlr
smlr --help
You should see an output similar to:
smlr 0.2.1
Wilfried Kopp <chevdor@gmail.com>
Hash data the same way Substrate-based chains such as Kusama or Polkadot.
This gives a hash matching proposal hashes onchain.
USAGE:
smlr [FLAGS] [INPUT]
FLAGS:
-h, --help Prints help information
-v Sets the level of verbosity
-V, --version Prints version information
ARGS:
<INPUT> Sets the input file to use. Alternatively, you may pipe <stdin>.
srtool articles: https://www.chevdor.com/tags/srtool
srtool repo: https://gitlab.com/chevdor/srtool
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.