LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   data file sort problem (https://www.linuxquestions.org/questions/linux-newbie-8/data-file-sort-problem-639075/)

johnpaulodonnell 05-01-2008 04:59 AM

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.

colucix 05-01-2008 06:52 AM

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.

johnpaulodonnell 05-01-2008 08:10 AM

thanks! - appreciate your help


All times are GMT -5. The time now is 12:56 AM.