LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2013, 12:58 PM   #1
wilelucho
LQ Newbie
 
Registered: Jun 2012
Location: Brazil
Distribution: Ubuntu
Posts: 19

Rep: Reputation: Disabled
count occurrences of numbers between specific values


Hi all,

I have 4018 files each has the latitude and longitude coordinates of daily weather reports, what I would like to do (if possible) is to calculate the number of weather stations within a region for each file. For example all points between latitude -20 and -10 and longitude -70 and -50. Each file (daily report) has a variable number of records

Thanks
Luis
 
Old 09-23-2013, 01:25 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin
 
Old 09-23-2013, 01:48 PM   #3
wilelucho
LQ Newbie
 
Registered: Jun 2012
Location: Brazil
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: Disabled
All right Daniel,
part of the file I have is like this
77777 -6.47 -35.86 0.00
77777 -2.73 -63.25 0.00
77777 -9.48 -35.85 0.00
77777 -10.49 -40.20 2.40
77777 -9.51 -36.02 0.00
77777 -8.52 -36.03 0.00
77777 -14.52 -64.81 0.00
77777 9.55 -69.24 0.00
77777 3.56 -40.65 0.00
77777 -7.62 -48.39 0.00
77777 -3.57 -68.28 4.10
77777 -4.59 -46.68 8.00
77777 0.59 -62.39 0.70
77777 -34.62 -37.76 0.00
77777 -23.62 -49.15 0.00
77777 -14.62 -67.53 4.60
77777 -16.63 -37.76 0.00
77777 -22.64 -56.02 0.00
77777 -29.67 -35.70 0.00
77777 -9.68 -40.60 0.00
77777 -33.68 -61.98 0.00
77777 -9.69 -65.99 36.70
77777 -5.70 -65.36 10.50
77777 -8.72 -35.89 0.00
77777 -6.75 -37.45 0.00
77777 9.75 -63.19 3.00
77777 -2.76 -66.61 35.70
77777 -13.82 -54.89 0.00
77777 -11.86 -58.25 0.00
77777 -9.87 -56.10 0.00
77777 -13.89 -62.99 0.00
77777 -17.89 -75.75 0.00
77777 -19.93 -63.06 0.00
77777 -25.94 -57.13 1.70
77777 -22.96 -47.67 2.60
77777 -32.97 -36.98 0.00
77777 -30.97 -37.00 0.00
77777 -34.97 -49.99 6.90
77777 -16.99 -47.48 13.80

What I would like is to get the number of occurrences that I have on the second column within -20 and -10 AND on the third column within -70 and -50, that might bring 6, since this is the number of records that match my request

77777 -13.82 -54.89 0.00
77777 -11.86 -58.25 0.00
77777 -13.89 -62.99 0.00
77777 -19.93 -63.06 0.00
77777 -14.62 -67.53 4.60
77777 -14.52 -64.81 0.00

Hope this can help me
Luis
 
Old 09-23-2013, 02:08 PM   #4
wilelucho
LQ Newbie
 
Registered: Jun 2012
Location: Brazil
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: Disabled
Never mind, found a solution with awk

Code:
awk ' $2>-20 && $2<-10 && $3>-70 && $3<-50' filename.dat
Thanks anyway, just asking makes my brain to speed up !

Luis
 
Old 09-23-2013, 04:32 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by wilelucho View Post
Code:
awk ' $2>-20 && $2<-10 && $3>-70 && $3<-50' filename.dat
Nice clean solution. However, your problem statement suggests there is more to be done.

You cited "the number of occurrences." This awk enumerates lines which meet certain criteria but does not count them.

You wrote of "the number of weather stations within a region." Where are those weather stations? Are their coordinates (latitude and longitude) in another file? What defines the size and shape of a region? Is a region rectangular or within a specified radius around each station?

awk has good arithmetic capabilities and the solution to a more complex problem could be built around a sequence of them.

Daniel B. Martin
 
Old 09-24-2013, 06:31 AM   #6
wilelucho
LQ Newbie
 
Registered: Jun 2012
Location: Brazil
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: Disabled
What I was after was that, the only thing I added was

Code:
 awk ' $2>-20 && $2<-10 && $3>-70 && $3<-50' filename.dat | wc -l
and that gives me what I was looking for, because the region is a rectangle and what I need is to know how many stations do I have in that particular region latitude min=-20, latitude max=-10, longitude min=-70, longitude max=-50. The file content are each station's latitude/longitude, so by counting how many meet this criteria, I'm obtaining my goal, to know for each day (each file) how many reporting stations do I have.

Anyway thanks for your interest Daniel !

Best regards
Luis
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count occurrences of unique words in a file beeblequix Linux - General 8 06-05-2018 10:16 AM
[SOLVED] count how many number occurrences in series kpinto Linux - Newbie 4 06-02-2011 08:22 AM
count values in java? rabbit2345 Programming 1 02-28-2008 06:57 AM
how to count the number of occurrences of a process beeblequix Linux - General 3 09-18-2006 04:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration