how to extract last line in record

Hi all!!

After experiencing great helpfulness the last time I posted a problem at this site, I once again turn to the forum for expert help.

The problem:

I have a file.dat containing x, y, and z coordinates, like:

x y z
1 1 4
1 2 3
1 3 9

2 1 7
2 2 2
2 3 8

3 1 3
3 2 5
3 3 7

.....and so on.

I would like to extract the maximum z value with its respective x value from each data block and insert these values into a filenew.dat:

Example:

file.dat

1 1 9 #extract this
1 2 3
1 3 2

2 1 7
2 2 2
2 3 8 #extract this

3 1 3
3 2 7 #extract this
3 3 5

and insert into....

filenew.dat
1 9
2 8
3 7

Already greatful for previous assistance, I humbly ask:
Could anybody help me with this?

Regards
bjorb

maybe you should try to use awk ? i'm sorry , i haven't programmed using awk for a long time but this is a little tutorial about awk.

Thanks for the link.
But to be honest I'm not to familiar with awk myself.
Is there a way to pick a max value from a field using awk?

Could grep be used?

bjorb

Hi all!!

I have a file containing records e.g. file.dat below.
Now I want to extract the last line from each record and insert this
into a new file, filenew.dat

file.dat
1 3 5
2 4 6
1 6 9 #extract this

3 9 4
3 8 2
8 4 7
4 7 2 #extract this

8 4 1
3 6 2 #extract this

....EOF...

Could anybody please help me with tihis?
Is there a way to use grep in this problem?

Regards
bjorb

I've merged the threads as the questions are very similar. For the first question, try...

tail +2 file.dat | awk -v RS= '
    $3>$6 && $3>$9 {print $1,$3}
    $6>$3 && $6>$9 {print $4,$6}
    $9>$6 && $9>$6 {print $7,$9}
'

...which gives...

1 9
2 8
3 7

For the second question...

awk -v RS= '{print $(NF-2),$(NF-1),$NF}' file.dat

For these kinds of problems you can't just use grep. You have to write some code. If you don't know awk, then use another language that you are familiar with.

Thanks Ygor.
When it comes to the first question (partly) I used ruby code (provided by futurelet on a previous question) to sort each record with respect to the z-value, resulting in the largest z-value at the bottom of each record.

Then I used the code given by you on the second question to extract the last line of each record and inserting this into a new file, producing a file with maximum z-values.

regards

bjorb

Hi Ygor!
Could you please explain the awk code for extracting last line in the record?
I tried to use the code on records containing only two fields, but then it only printed the last line from the last record. Why?

regards
bjorb

RS is awk's input record separator. Its default value is a string containing a single newline character, which means that an input record consists of a single line of text. (See How Input is Split into Records .)

The empty string, "" (a string of no characters), has a special meaning as the value of RS: it means that records are separated only by blank lines. See Multiple-Line Records, for more details.

NF is the number of fields in the current input record. NF is set each time a new record is read, when a new field is created, or when $0 changes (see Examining Fields).

The last field in a record is $NF, the second last is $(NF-1).