LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to count the numerical digits in between the text using a command or a script? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-count-the-numerical-digits-in-between-the-text-using-a-command-or-a-script-779184/)

Kilam orez 01-01-2010 12:20 AM

how to count the numerical digits in between the text using a command or a script?
 
hi dear all,

i want to count the digits in between the text in a file.

e.g.

write down the form

2.3 3.3 3.0 505.0 0.777E-07

22.3 3.3 5.0 503.0 1.777E-04

then read this
.

how can i do the counting of these digits present in between a text in a file?

with regards

kilam

ghostdog74 01-01-2010 01:42 AM

what should your total count be? explain in detail how you are counting the digits. Homework?

Kilam orez 01-01-2010 02:57 AM

Quote:

Originally Posted by ghostdog74 (Post 3810575)
what should your total count be? explain in detail how you are counting the digits. Homework?

Actually i have a very big file which contains many points defined by a mathematical function. These point are separated by text, like given in previous thread. But the number of points are very large, more than thousands.

i just want to count the number of points.Also these points are arranged horizontally like given below:

0.135079299943E-03 0.137325385988E-03 0.139599724318E-03 0.141902670302E-03 0.144234583780E-03 0.146595829119E-03 0.148986775268E-03 0.151407795817E-03 and so on.


hope i explained well.

with regards

kilam

pixellany 01-01-2010 08:31 AM

From what you have supplied, it sounds like you want to count NUMBERS and not digits. Each number you show is in scientific notation, which includes a character.

Please clarify exactly what you need to count----for example, in the post above, there are 8 numbers, each with 12 significant digits + a 2-digit exponent. Since the leading digit is always 0, I do not count it as significant.

tredegar 01-01-2010 09:31 AM

Quote:

i just want to count the number of points.Also these points are arranged horizontally like given below:
So I think he wants to count the total number of numbers.

I expect he could easily do this by counting the total number of occurrences of the letter E as each number has only one E.

Or use tr to convert all the spaces to newlines, grep to filter out blank lines, then just count the number of lines in the file?

Here's an interesting link:
http://www.linuxjournal.com/article/10359

[Edit] It's even easier than that:

The numbers are alphanumeric strings, separated by whitespace. So they count as "words" which can be counted by wc

So to count the number of numbers all you need is wc -w filename

[/Edit]

Kilam orez 01-02-2010 04:34 AM

Quote:

Originally Posted by tredegar (Post 3810820)
So I think he wants to count the total number of numbers.

I expect he could easily do this by counting the total number of occurrences of the letter E as each number has only one E.

Or use tr to convert all the spaces to newlines, grep to filter out blank lines, then just count the number of lines in the file?

Here's an interesting link:
http://www.linuxjournal.com/article/10359

[Edit] It's even easier than that:

The numbers are alphanumeric strings, separated by whitespace. So they count as "words" which can be counted by wc

So to count the number of numbers all you need is wc -w filename

[/Edit]

dear tredegar,

sorry i didn't tell that my file contains numbers along with the numbers in scientific notation.
i.e.

-0.968629524999E-01 -0.980905826664E-01 -0.993336540375E-01 -0.100592360821 -0.101866899668

so, counting the number of E's works fine using wc command, but how to count both types of numbers?

with regards,

kilam

pixellany 01-02-2010 04:57 AM

You could count the occurences of the following Regex:
Code:

0\.[0-9]{5}[0-9]*[^0-9]
("0", then literal ".", then a minimum of 5 numerals, then a non-numeral)

tredegar 01-02-2010 05:22 AM

Like I said in my edit - you don't need to count the number of Es. The problem is trivial.

Just use wc -w filename

You could test this by copying a small part of your file to another file, manually counting the numbers, then
wc -w testfile

ghostdog74 01-02-2010 05:25 AM

Code:

$ more file
text -0.968629524999E-01 -0.980905826664E-01 -0.993336540375E-01 -0.100592360821 -0.101866899668 text
$ awk '{for(o=1;o<=NF;o++)if($o+0==$o) ++d}END{print d}' file
5


sonu kumar 01-03-2010 12:15 AM

Quote:

Originally Posted by tredegar (Post 3811506)
Like I said in my edit - you don't need to count the number of Es. The problem is trivial.

Just use wc -w filename

You could test this by copying a small part of your file to another file, manually counting the numbers, then
wc -w testfile

thank you


All times are GMT -5. The time now is 07:45 PM.