how to extract last line in record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to extract last line in record
# 1  
Old 10-12-2005
extract specific data from file and insert into new file.

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
# 2  
Old 10-12-2005
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.
# 3  
Old 10-13-2005
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
# 4  
Old 10-13-2005
how to extract last line in record

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
# 5  
Old 10-13-2005
I've merged the threads as the questions are very similar. For the first question, try...
Code:
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...
Code:
1 9
2 8
3 7

For the second question...
Code:
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.
# 6  
Old 10-13-2005
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
# 7  
Old 10-13-2005
This is for Ygor

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

2. Shell Programming and Scripting

Extract record from file based on section.

input file output file (1 Reply)
Discussion started by: lathigara
1 Replies

3. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

4. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

5. Shell Programming and Scripting

Script to extract particular record

Hi, I have a large file with huge number of records which are of following pattern: TYPE1 { originNodeType : "IVR" originHostName : "AAIVR" originTransactionID : "01310559" originTimeStamp : "20110620192440+0530" hostName : "hhhh" voucher : '0'D rProfileID : "ZZZZ" Before { Flags :... (1 Reply)
Discussion started by: madhukar1anand
1 Replies

6. Shell Programming and Scripting

Unix command to extract a record from a table

Suppose there is a table like the following...I just wanted to know if there is any command using which we can get the record/name of the person who joined before 2005.. Sl Name des y.o.joining 1 Ram Engineer 2001 2 Hari Doctor 2004 3 David Plumber 2005 4 Rahim painter 2007 5 gurmeet... (1 Reply)
Discussion started by: satyajit007
1 Replies

7. Shell Programming and Scripting

PERL: Extract random record which has 4 lines each

Hi, I have a data file with millions of record (N). Each record was saved in 4 lines. So there are total of NX4 lines in the data file. For Example: Host1 a b c d Host2 e f g h Host3 i j k (2 Replies)
Discussion started by: phoeberunner
2 Replies

8. Shell Programming and Scripting

How to extract first and last line of different record from a file

Hi all I want to inquire that is there any unix command that can help me while extracting first and last line in a file ( TEST.dat) for example in the below record i want to extract the line that are in BOLD text or in other words i want to extract line no 1,3,4 and 7 aa 1 2 3 aa 2 3 4... (5 Replies)
Discussion started by: Bungash125
5 Replies

9. UNIX for Dummies Questions & Answers

Using sed to extract Nth record?

I have a comma-separated record and I'd like to use sed to pull the Nth record from it. It seems like it'd need to be something like this: sed -n 's/'"\,$1\,"'/&/p' Am I close? (3 Replies)
Discussion started by: doubleminus
3 Replies

10. UNIX for Dummies Questions & Answers

How to extract duplicate records with associated header record

All, I have a task to search through several hundred files and extract duplicate detail records and keep them grouped with their header record. If no duplicate detail record exists, don't pull the header. For example, an input file could look like this: input.txt HA D1 D2 D2 D3 D4 D4... (17 Replies)
Discussion started by: run_eim
17 Replies
Login or Register to Ask a Question