The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK - if last line/record do something PacificWonder Shell Programming and Scripting 3 06-05-2008 08:13 PM
Extract a line from a file using the line number zambo Shell Programming and Scripting 1 05-01-2008 10:39 AM
Using sed to extract Nth record? doubleminus UNIX for Dummies Questions & Answers 3 04-19-2008 07:18 AM
extract a line from a file using the line number grandtheftander Shell Programming and Scripting 6 02-06-2008 03:12 AM
How to extract duplicate records with associated header record run_eim UNIX for Dummies Questions & Answers 17 01-16-2007 08:46 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-12-2005
Registered User
 

Join Date: Sep 2005
Posts: 12
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
Reply With Quote
Forum Sponsor
  #2  
Old 10-12-2005
krhamidou's Avatar
Registered User
 

Join Date: Oct 2005
Posts: 27
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.
Reply With Quote
  #3  
Old 10-12-2005
Registered User
 

Join Date: Sep 2005
Posts: 12
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
Reply With Quote
  #4  
Old 10-13-2005
Registered User
 

Join Date: Sep 2005
Posts: 12
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
Reply With Quote
  #5  
Old 10-13-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
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.
Reply With Quote
  #6  
Old 10-13-2005
Registered User
 

Join Date: Sep 2005
Posts: 12
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
Reply With Quote
  #7  
Old 10-13-2005
Registered User
 

Join Date: Sep 2005
Posts: 12
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0