![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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}
'
Code:
1 9 2 8 3 7 Code:
awk -v RS= '{print $(NF-2),$(NF-1),$NF}' file.dat
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |