The UNIX and Linux Forums  

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
duplicate rows in a file infyanurag Shell Programming and Scripting 3 05-22-2008 01:39 AM
Using Sed to duplicate a section of a file.... nick26 Shell Programming and Scripting 2 05-01-2008 12:18 PM
How to obtain system open file table value in Linux mahesh. Shell Programming and Scripting 0 05-25-2007 11:25 AM
arrow keys / special keys raguramtgr UNIX for Dummies Questions & Answers 3 02-20-2004 02:45 AM
Get a none duplicate list file trynew Shell Programming and Scripting 5 06-25-2002 01:06 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 07-27-2007
oscarmon oscarmon is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 6
obtain duplicate keys in csv file

Hi,
having two csv files, both sorted, by key (column1), f1 containing duplicate keys and f2 containing no duplicate keys, how can I obtain all rows from f1 with the keys listed in file2? Example:

f1 is:
k1,gsj01fd
k2,vi982cj
k2,1fjk01e
k3,81kjfds
k4,sd9dasi

f2 is:
k2
k3

and I would like to obtain f3 like:
k2,vi982cj
k2,1fjk01e
k3,81kjfds

Thanks.
  #2 (permalink)  
Old 07-27-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk -F, -v OFS=',' 'FNR==NR{a[$1];next} a[$1]' f2 f1

  #3 (permalink)  
Old 07-27-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149

Code:
join -j 1 -t, f1 f2

  #4 (permalink)  
Old 07-27-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960

Code:
awk -F"," 'BEGIN{ while ((getline < "f2" ) > 0 ) arr2[i++]=$0 } { for ( x in arr2 ) { if ( match(arr2[x], $1) ) { print; last } } }' f1

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 06:54 PM.


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