Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

1000_A1\tB1\n
___1_A2\tB2\n

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

1000\tA1\tB1\n
1\tA2\tB2\n

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
1  
Are you talking about sort | uniq -c? –  John Flatness Oct 11 '11 at 2:17
add comment

3 Answers 3

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
add comment

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
add comment

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}'
5
10
$ 
share|improve this answer
add comment

Your Answer

 
discard

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.