I want to write a script in bash that prints the least repeating line of standard input
I wrote this code:
#!/bin/bash
var=1000
while read line
do
tmp=$(grep -c $line)
if [ $tmp -lt $var ]
then
var=$tmp
out=$line
fi
done
var="$var $out"
echo $var
but e.g. when using a test like this
id1
id2
id3
id1
square
id1
id2
id3
id1
circle
id2
id2
the program only enters the loop once thus it gives a bad output
3 id1
when the correct one should be
1 square
This line
tmp=$(grep -c $line)
seems to be breaking the loop but I can't find out why. Is there any way to bypass using grep in my code or any other way to fix my script?
circleyour expected output? It is neither the last repeating nor the last unique line in your example.