FabioC
FabioC

Reputation: 41

Find numbers in file with grep

I have a file with numbers, from 1 to 25.

How to find how many times each number occurs in this file?

I am using shell script to do it, so grep makes sense for me:

grep char -o filename | wc -l

But, I have a problem. In this sequence :

1,2,3,4,5,6
11,22,33,44,55,66
11,11,11,1,1,1,1

This command will find number 11 4 times, great ! This is an accurate answer.

But, will find number 1 13 times ! And number 1 just occurs 5 times.

So, the answer I am trying to find is: How to tell to grep that 1 is different of 11, and 2 is different of 22, and so on?

Upvotes: 1

Views: 2528

Answers (3)

Kyle Banerjee
Kyle Banerjee

Reputation: 2784

cat filename |sed 's/,/\n/g' |sort -n |uniq -c

The sed statement replaces all the commas so now you have a stream consisting of one number per line. The result is sorted numerically and then counted using the uniq utility

You're welcome.

~# cat filename |sed 's/,/\n/g' |sort -n |uniq -c                    
      5 1
      1 2
      1 3
      1 4
      1 5
      1 6
      4 11
      1 22
      1 33
      1 44
      1 55
      1 66

Upvotes: 0

anubhava
anubhava

Reputation: 784898

Use word boundaries around search pattern:

grep -o '\<1\>' file | wc -l
5

Upvotes: 0

fedorqui
fedorqui

Reputation: 289495

You need to add -w in the grep command.:

$ grep -wo 1 file | wc -l
5

This way, it will just match full words and for example 13 won't be matched when looking for 1.

From man grep:

-w, --word-regexp

Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

Upvotes: 1

Related Questions