Changing delimiter of the uniq command

eran picture eran · Jun 10, 2013 · Viewed 10.1k times · Source

I'd like the output of the uniq command to be comma separated, so that instead of:

     30 hello
     31 world
     36 hey_there
    142 i_am_bigest

I'll get:

30,hello
31,world
36,hey_there
142,i_am_biggest

My input has no spaces, but just using sed or tr can be a problem since the number of leading spaces varies according to the number of decimal digits in the count.

Answer

nneonneo picture nneonneo · Jun 10, 2013

Pipe the output to

sed -e 's/^ *//;s/ /,/'

This first removes the leading spaces (^ *) then replaces the first space with a comma.