0

I have a file like

a
A
b
c

I want to print number of times each line repeats it self in a case insensitive manner. I found below command to be useful:

tr A-Z a-z | uniq -c | sed -e 's/^ *//' -e 's/ *$//'

But it fails if the file is of form A \n a. This gives 2 a. But the expected output is 2 A

How can I do that?

1 Answer 1

1

First of all you have to use sort command before using uniq -c because this command fails if you have another repeated characters after other characters like input:

a
A
b
c
A
a
a

And second problem you get wrong result because you convert all uppercase to lowercase, it cause you getting result in lowercase.

Also you don't need 's/ *$//' part of sed because your output has no spaces on end of lines.

You should try with your modified command as this:

tr a-z A-Z <file |sort | uniq -c | sed 's/^ *//'

Or even shorter try:

sort -r file |uniq -ci

sort with its -r option sorts Uppercase character as first rows. and with uniq and its -i option ignores differences in case.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .