Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help arranging large data file 1

Status
Not open for further replies.

chep80

Technical User
Jul 22, 2005
10
US
I have a data file that looks like this:

PATH Trial CTR LAT CTR LON
1 1 80.774791 3.21062
1 2 80.021757 -4.274059
1 3 79.129883 -10.647337
1 4 78.130377 -16.029408
1 5 77.048078 -20.572231
2 6 75.901981 -24.423146
2 7 74.706362 -27.710197
2 8 73.4719 -30.538982
2 9 72.20661 -32.99444
2 10 70.916558 -35.14418
3 11 69.606373 -37.041893
3 12 68.279631 -38.730311
3 13 66.939121 -40.243615
3 14 65.587044 -41.609314
3 15 64.225153 -42.849707
4 16 62.854858 -43.982999
4 17 61.477307 -45.024165
4 18 60.093439 -45.985606
4 19 58.704028 -46.877669
4 20 57.309721 -47.709041
5 21 55.911059 -48.487062
5 22 54.5085 -49.217972
5 23 53.102434 -49.907108
5 24 51.693192 -50.559055
5 25 50.281063 -51.177779
6 26 48.866293 -51.76672
6 27 47.449102 -52.328882
6 28 46.029677 -52.866897
6 29 44.608187 -53.383082

And the "PATH" value continues up to 195. What I need to do is take this data and rearrange it such that the path and its corresponding data to the right of it is in this order:

3
3
3
3
3
19
19
19
19
19
35
35
35
35
35
51
51
51
51
51

(continue to increment by 16)

195
195
195
195
195

I'm only a beginner and very new to Perl scripting, so any help is greatly appreciated. Thanks
 
Code:
#!/usr/bin/perl -w
use strict;
print scalar(<>);
while(<>) {
  /^(\d+)/
    and (($1 % 16) == 3)
    or next;
  print;
}




(very injured) Trojan.
 
i hate to do this - but this does scream for an awk solution (and Trojan's already upset me tonight!)

Code:
awk '$1 % 16 == 3 {print $0}' data.txt

... now please don't give me grief. I would love to see this solution even in a Perl forum - simply because i might not realise it was even possible. Unless i have cocked it up...


Kind Regards
Duncan
 
Are we sure that we don't need brackets now then?
Have we tested, tested, tested? ;-)




(very injured) Trojan.
 
sod off! - you're just jealous that it's neater & more concise than yours! ;-)


Kind Regards
Duncan
 
doesn't need testing... it's pure genius (i wish)


Kind Regards
Duncan
 
isn't Perl pretty forgiving of this already?

Code:
[b]#!/usr/bin/perl[/b]

@values = ( '1',
            '2',
            ' 3',
            '          4',
            '     5              ',
            '6six',
            'seven7',
            '  8  ' );
            
foreach $value (@values) {
  print $value * 2 . "\n";
}

outputs:-

2
4
6
8
10
12
0 - only problem here on the '7' ... coming after textual characters
16


Kind Regards
Duncan
 
My "print scalar(<>)" is to stop "print" offering a list context to "<>" and consequently slurping the whole file.

BTW: the brackets in "($1 % 16) == 3" was to stop this being seen by perl as "$1 % (16 == 3)".




(very injured) Trojan.
 
Ah! I see... apologies

right - it's print scalar (11 p.m.)

... and i'm off to bed!

Goodnight dudes!


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top