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-01-2008, 05:59 AM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
data file sort problem


Hi.

I'm working with a seismic data file.

$1 : earthquake number
$2 : seismometer
$3 : backazimuth of earthquake relative to seismometer

The number of seismometers which pick up the earthquake signal depends on, among other things, it's magnitude. This means I have a data file that looks something like:

1 a 65.3
1 b 65.2
1 c 65.1
2 b 88.7
2 d 88.5
3 b 125.1
3 c 125.2
3 d 125.3
3 e 125.4
etc....


where a,b,c,d,e are the seismometers.

I'd like to sort the data according to backazimuth range - eg extract all earthquakes coming from the backazimuth range 60 - 80. This is easily done in awk.

My problem is that the code to which I'm passing the data file (which I don't want to tinker with!) requires the earthquake index to run from 1 - n continuously.

So, just taking the first column, after extraction of certain events it might look like:

1
1
4
4
4
4
7
7
7
11
11
11
11
11
11

etc...

Does anyone know how I could re-number this index to run from 1 - n continuously? ie so the above would become:

1 -> 1
1 -> 1
4 -> 2
4 -> 2
4 -> 2
4 -> 2
7 -> 3
7 -> 3
7 -> 3
11 -> 4
11 -> 4
11 -> 4
11 -> 4
11 -> 4
11 -> 4

Thanks.
 
Old 05-01-2008, 07:52 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
In your awk code you can insert something like this, for lines that match the requirements and have to be printed
Code:
{ 
  if ($1 > number)
     print ++count,$2,$3
  else
     print count,$2,$3
  number=$1
}
that is, each time $1 has an increment the count is incremented and printed, otherwise the current value of count is printed. I assume that not assigned variables (number, count) have a value of zero, but this may be different in other awk versions. If this is the case, just initialize them to zero in the BEGIN section of your awk program.
 
Old 05-01-2008, 09:10 AM   #3
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
thanks! - appreciate your help
 
  


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
How sort data in some file which are greater than some value? sarajevo Programming 5 08-25-2006 04:48 AM
some sort of data analyzer exkor5000 Linux - Software 2 07-07-2006 10:52 AM
file system problem? data lost Tinku Linux - General 3 11-26-2005 07:21 PM
Unix data sort question amytys Programming 2 07-01-2005 06:24 AM
Redhat Data Sort eivets Linux - Software 3 03-22-2005 03:52 PM


All times are GMT -5. The time now is 04:04 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration