Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

join based on line number when one file is missing lines

Shell Programming and Scripting


Tags
join

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-27-2011   -   Original Discussion by jackiev
jackiev's Unix or Linux Image
jackiev jackiev is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 27 June 2011, 7:09 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like:


Code:
1 200.3 -0.3
2 201.7 -0.32
...
87 200.2 -0.314

I have another file which contains data that was taken at certain of these 87 positions. i.e.:



Code:
37 125
42 175
86 142

where the first number is the line number of the coordinates in the other file. Is there a way to get an output like:



Code:
37 201.2 -0.324 125
42 200.1 -0.311 175
86 203.0 -0.35 142

where I am pulling columns 2 & 3 from the first file with 87 lines, and column 4 from the second file (in this example with 3 lines but I want to be able to do more)?

I've tried using join to do this but it doesn't bring up anything!

Thanks!
Moderator's Comments:
join based on line number when one file is missing lines Please use code tags when posting data and code samples!

Last edited by vgersh99; 06-27-2011 at 07:09 PM.. Reason: code tags, please!
Sponsored Links
    #2  
Old Unix and Linux 06-27-2011   -   Original Discussion by jackiev
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 21 May 2018, 12:25 PM EDT
Location: Foxborough, MA
Posts: 8,278
Thanks: 244
Thanked 819 Times in 765 Posts


Code:
nawk 'FNR==NR{data[$1]=$2;next} $1 in data {print $0, data[$1]}' dataFile coordinatesFile

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge two non-consecutive lines based on line number or string munkee Shell Programming and Scripting 7 05-17-2011 06:33 PM
Delete lines based on line number novice_man Shell Programming and Scripting 2 01-28-2011 05:56 AM
Join in a single line variable number of lines cgkmal Shell Programming and Scripting 4 08-03-2009 06:52 AM
Split File Based on Line Number Pattern shankster Shell Programming and Scripting 11 10-01-2008 09:49 AM
How can I get an if statement to execute based on number of lines in a file? LordJezo Shell Programming and Scripting 6 05-14-2004 10:50 AM



All times are GMT -4. The time now is 12:43 PM.