Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-01-2010, 12:20 AM
|
#1
|
Member
Registered: Aug 2009
Location: India
Distribution: open suse, fedora
Posts: 33
Rep:
|
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
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
01-01-2010, 01:42 AM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
what should your total count be? explain in detail how you are counting the digits. Homework?
|
|
|
01-01-2010, 02:57 AM
|
#3
|
Member
Registered: Aug 2009
Location: India
Distribution: open suse, fedora
Posts: 33
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
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
|
|
|
01-01-2010, 08:31 AM
|
#4
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
|
1 members found this post helpful.
|
01-01-2010, 09:31 AM
|
#5
|
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora40
Posts: 6,153
|
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]
Last edited by tredegar; 01-01-2010 at 10:45 AM.
|
|
2 members found this post helpful.
|
01-02-2010, 04:34 AM
|
#6
|
Member
Registered: Aug 2009
Location: India
Distribution: open suse, fedora
Posts: 33
Original Poster
Rep:
|
Quote:
Originally Posted by tredegar
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
|
|
|
01-02-2010, 04:57 AM
|
#7
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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)
|
|
1 members found this post helpful.
|
01-02-2010, 05:22 AM
|
#8
|
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora40
Posts: 6,153
|
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
|
|
|
01-02-2010, 05:25 AM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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
|
|
1 members found this post helpful.
|
01-03-2010, 12:15 AM
|
#10
|
LQ Newbie
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28
Rep:
|
Quote:
Originally Posted by tredegar
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 08:18 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|