Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uniq -c without additional spaces

Tags:

shell

uniq

Is there an option in uniq -c (or an alternative) that doesn't add additional whitespaces around the count number? Currently I generally pipe it through sed, like so:

sort | uniq -c | sed 's/^ *\([0-9]*\) /\1 /'

But this seems kinda redundant, particularly given how frequently I have to do this.

like image 333
JohnnyTooBad Avatar asked Oct 18 '25 13:10

JohnnyTooBad


1 Answers

You can try to make the sed command as short as possible with

sort | uniq -c | sed 's/^ *//'

If you have GNU grep, you can also use the -P flag:

sort | uniq -c | grep -Po '\d.*'

(Do not use awk '{$1=$1};1', it will trim more than you want)

When you need this often, you can make a function or script calling

sort | uniq -c | sed 's/^ *//'

or only

uniq -c | sed 's/^ *//'
like image 70
Walter A Avatar answered Oct 22 '25 06:10

Walter A