Changing delimiter of the uniq command

man uniq (at least on Mac OS X, aka BSD) does not give any way to handle that. Your best bet is probably sed:

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

The output from uniq -c consists of some blanks, a number, a blank, and the input string.

The basic idea is that the sed script looks for an arbitrary number of blanks, a number and a blank, and replace it by the number and a comma. Looking at the POSIX specification for uniq, the output is not supposed to have leading blanks (the printf() format should be "%d %s"), but leading blanks are normal in practice (for small enough repeat counts; on Mac OS X, the output printf() format is effectively "%5d %s").


pipe the output to :

perl -lane '{print join ",", @F}'

  1. Using printf works:

    xargs -L 1 printf '%s,%s\n' < file
    
  2. Using bash:

    printf '%s,%s\n' $(<file)
    
  3. In a POSIX shell this would also work:

    printf '%s,%s\n' $( ...various commands... | uniq -c )
    

Pipe the output to

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

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

Tags:

Uniq