LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-14-2012, 04:03 PM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
awk while loop over two variables


Given two lat, lon coordinates, I'd like to output a file with intermediate coordinates at increments of 0.5 (say). So using the coordinates pair (38, 60) and (40, 62) as an example, I'd like to get:

Code:
38.00  60.00
38.00  60.50
38.00  61.00
38.00  61.50
38.00  62.00

38.50  60.00
38.50  60.50
38.50  61.00
38.50  61.50
38.50  62.00

39.00  60.00
39.00  60.50
39.00  61.00
39.00  61.50
39.00  62.00

39.50  60.00
39.50  60.50
39.50  61.00
39.50  61.50
39.50  62.00

40.00  60.00
40.00  60.50
40.00  61.00
40.00  61.50
40.00  62.00
(Blank lines are included here for ease of visibility)

Using the code snippet below, the lat variable (i) doesn't increment, and the program terminates giving just:

Code:
38.00  60.00
38.00  60.50
38.00  61.00
38.00  61.50
38.00  62.00

Code:
 
set lat1 = 38
set lat2 = 40
set lon1 = 60
set lon2 = 62
set increment = 0.5
	
awk 'BEGIN { i = '"$lat1"'; j = '"$lon1"'; while (i <= '"$lat2"') {while (j <= '"$lon2"') {printf "%5.2f  %5.2f\n", i, j; j = j + '"$increment"'}; i = i + '"$increment"'}}'
Any help would be appreciated.
 
Old 05-14-2012, 04:14 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Your problem is that the j get's incremented to the max on the first run of i,
and never reset ....

Code:
echo ""|awk -v lat1=38.00 -v lon1=60.00 -v lat2=40.00 -v lon2=62.00 -v increment=0.5 -f lat.awk 
38.00  60.00
38.00  60.50
38.00  61.00
38.00  61.50
38.00  62.00
38.50  60.00
38.50  60.50
38.50  61.00
38.50  61.50
38.50  62.00
39.00  60.00
39.00  60.50
39.00  61.00
39.00  61.50
39.00  62.00
39.50  60.00
39.50  60.50
39.50  61.00
39.50  61.50
39.50  62.00
40.00  60.00
40.00  60.50
40.00  61.00
40.00  61.50
40.00  62.00
Code:
cat lat.awk
BEGIN{ 
  i = lat1; 
  j = lon1; 
  while (i <= lat2) {
    while (j <= lon2) {
      printf "%5.2f  %5.2f\n", i, j; 
      j = j + increment
    }; 
    j = lon1
    i = i + increment
  }
}

Cheers,
Tink
 
Old 05-14-2012, 04:22 PM   #3
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] concatenate 2 variables in dialog using awk while in for loop haraldboehmecke Programming 5 11-25-2011 08:45 AM
Loop with variables sebastiaankop Linux - Newbie 2 01-06-2011 09:35 AM
Loop with 2 variables tensigh Programming 7 11-17-2009 08:07 PM
Using for loop on 2 variables muazfarooqaslam Linux - Newbie 8 02-01-2008 09:03 AM
shell variables becoming zero outside the loop cool244 Programming 4 05-20-2006 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:45 AM.

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