Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

uniq -c separate the occurrence number by spaces, which is hard for to cut or awk to separate it out later.


I can solve this problem using sed -r 's/^ *([0-9]+)/\1\t/' to change the delimiter to tab. Then cut -f1 could return:


But it seems a common usage to have uniq -c separate the number by tab. Why is this feature missing? Is there any other easier way to do so?

share|improve this question
Doesn't "-c" just check whether the input is sorted? Where are you getting the output "1000 Field A", etc... from? – up_the_irons Oct 10 '11 at 7:52
Are you talking about sort | uniq -c? – John Flatness Oct 11 '11 at 2:17
up vote 0 down vote accepted

Sounds like you've got a format like <number><space><field 1 name><tab><field 2 name>, and you want to check that the input is sorted by field 1 name. If that's what you want, simply remove the initial number part and check the sorting of the remaining part of the first column:

echo "$input" | sed -r 's/^ *[^ ]+ //' | sort -c -k1,1
share|improve this answer

I'm not sure why you think it's hard for awk to separate the number. awk '{print $1}' prints it without a problem.

$ sort file | uniq -c
      5 x
     10 y
$ sort file | uniq -c | awk '{print $1}'
share|improve this answer

You can easily use cut to separate them out. Just use cut -d " " -f 1 to get the number and cut -d " " -f 2- to get the rest. Awk will just process it as it is whitespace.

share|improve this answer
cut -d " " -f 1 doesn't always cut the number out. Note that it's "___1_A2\tB2\n". – Cheng Nov 9 '11 at 11:37

If data columns can contain spaces you can use sed to replace space with a tab.

sed -e 's/<5 white space chars>//' | sed -e 's/<1 white space chars>/\t/'

First sed replaces leading white spaces generated by -c switch and the second one replaces white space between count and first data column.

share|improve this answer

may be this will easily work !!!

sort input_file | uniq -c | awk {'print $1 "\t" $2'} > tab_output
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.