The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Joining 2 CSV files together chachabronson UNIX for Advanced & Expert Users 3 05-06-2008 06:42 AM
Help with joining two files rjlohman Shell Programming and Scripting 3 09-27-2006 11:55 AM
Joining two or more binary files together Boothy UNIX for Dummies Questions & Answers 5 02-07-2006 09:10 PM
joining files Manu UNIX for Dummies Questions & Answers 2 04-25-2005 12:24 PM
joining 2 files webtekie UNIX for Dummies Questions & Answers 1 10-21-2003 10:51 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-03-2008
rochitsharma rochitsharma is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 82
Problem joining 2 files

Hi

I have 2 files which look like

File1

1245
1256
2345
2165
3245
9851
8514
6498
8547
2541
5242

and File2

2165,req,8421513,num,,,8475
8547,req,8484572,num,,,7548
3987,req,7638975,num,,,7483
1118,req,2423543,num,,,1254
5242,req,3452534,num,,,3245
1245,req,5423156,num,,,2345
.
.
.
and so on



Now all entries of File1 are present in File2 in first column.
I need to make a third file consisting of all the entries of File1 and there corresponding details from File2.
PS: File2 has many entries not in File1, and we don't need those entries in output file.

The output File for above sample of files is:

File3

1245,req,5423156,num,,,2345
2165,req,8421513,num,,,8475
8547,req,8484572,num,,,7548
5242,req,3452534,num,,,3245
.
.
.
.
and so on.


Thanks in advance.

Regards
Rochit
  #2 (permalink)  
Old 04-03-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
join command

Did you try the join command?
Code:
join File1 File2
By default, the first field will be taken as the join key.
  #3 (permalink)  
Old 04-03-2008
rochitsharma rochitsharma is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 82
But the problem is that File 2 has many extra entries and all are placed randomly........
  #4 (permalink)  
Old 04-03-2008
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
check this

Code:
awk -F, 'NR==FNR {a[$1]=$0} NR!=FNR{print a[$1]}' file2 file1 | sed '/^$/d'
where,
file2 --> the second file
file1 --> the first file
  #5 (permalink)  
Old 04-03-2008
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,854
If the following command doesn't work:

Code:
fgrep -f file1 file2
use this:

Code:
awk -F, 'NR==FNR{x[$1];next}$1 in x' file1 file2
Use nawk or /usr/xpg4/bin/awk on Solaris.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0