Counting the number of ten-digit numbers in a file

I need to count the total number of instances where the 10 digit number appears in the file. All numbers have leading zeros, for example:

This is some text. 0000000001

      

Return:

1

      

If the same number appears more than once, it is counted again, for example:

0000000001 This is some text.
0000000010 This is some more text.
0000000001 This is some other text.

      

Return:

3

      

Sometimes there are no spaces between the numbers, but each continuous line of 10 digits must be counted:

00000000010000000010000000000100000000010000000001

      

Return:

5

      

How can I determine the total number of ten-digit numbers displayed in a file?

+3


source to share


4 answers


Try the following:



grep -o '[0-9]\{10\}' inputfilename | wc -l

      

+19


source


The last requirement - you have to count multiple numbers per line - eliminates grep, as far as I know it can only count on a line.

Edit: Obviously I am fixed by Nate :). grep -o

is what I was looking for.



You can do it easily with the sed

following:

$ cat mkt.sh 
sed -r -e 's/[^0-9]/./g' -e 's/[0-9]{10}/num /g' -e 's/[0-9.]//g' $1
$ for i in *.txt; do echo --- $i; cat $i; echo --- number count; ./mkt.sh $i|wc -w; done
--- 1.txt
This is some text. 0000000001

--- number count
1
--- 2.txt
0000000001 This is some text.
0000000010 This is some more text.
0000000001 This is some other text.

--- number count
3
--- 3.txt
00000000010000000010000000000100000000010000000001

--- number count
5
--- 4.txt
1 2 3 4 5 6 6 7 9 0
11 22 33 44 55 66 77 88 99 00
123456789 0

--- number count
0
--- 5.txt
1.2.3.4.123
1234567890.123-AbceCMA-5553///q/\1231231230
--- number count
2
$ 

      

+2


source


"I need to count the total number of instances in which a 10-digit number appears within a file. All of the numbers have leading zeros"

So I think this might be more accurate:

$ grep -o '0[0-9]\{9\}' filename | wc -l

      

+1


source


This might work for you:

cat <<! >test.txt
0000000001 This is some text.
0000000010 This is some more text.
0000000001 This is some other text.
00000000010000000010000000000100000000010000000001
1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 0 j
12345 67890 12 34 56 78 90
!
sed 'y/X/ /;s/[0-9]\{10\}/\nX\n/g' test.txt | sed '/X/!d' | sed '$=;d'
8

      

+1


source







All Articles