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 fields from files with duplicate lines

Shell Programming and Scripting


Tags
duplicate, fields, join, unix

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-12-2012   -   Original Discussion by xan.amini
xan.amini's Unix or Linux Image
xan.amini xan.amini is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 12 December 2012, 4:03 PM EST
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Code Join fields from files with duplicate lines

I have two files,

file1.txt:


Code:
1 abc
2 def
2 dgh
3 ijk
4 lmn

file2.txt


Code:
1 opq
2 rst
3 uvw

My desired output is:


Code:
1 abc opq
2 def rst
2 dgh rst
3 ijk uvw

I have tried using the 'join' command (ie. 'join file1.txt file2.txt') but it returns the error that file1.txt is not sorted because of the repeated value (ie. 2).

Does anyone know how to solve this? Thanks!

Last edited by Franklin52; 12-13-2012 at 03:00 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old Unix and Linux 12-12-2012   -   Original Discussion by xan.amini
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 10 May 2018, 11:06 AM EDT
Location: Galactic Empire
Posts: 3,670
Thanks: 257
Thanked 1,330 Times in 1,246 Posts
I am getting your desired output running:-


Code:
join file1.txt file2.txt
1 abc opq
2 def rst
2 dgh rst
3 ijk uvw

Re-try specifying the field number:-


Code:
join -1 1 -2 1 file1.txt file2.txt
1 abc opq
2 def rst
2 dgh rst
3 ijk uvw

Sponsored Links
    #3  
Old Unix and Linux 12-12-2012   -   Original Discussion by xan.amini
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 22 May 2018, 11:01 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts
for unsorted file1.txt try:


Code:
awk 'NR==FNR{a[$1]=$2;next;}a[$1]{print $0,a[$1]}' file2.txt file1.tx

for all lines out try:


Code:
awk 'NR==FNR{a[$1]=$2;next;}{print $0,a[$1]}' file2.txt file1.txt

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
awk program to join 2 fields of different files abhisheksunkari Shell Programming and Scripting 4 10-05-2012 02:44 PM
Find duplicate based on 'n' fields and mark the duplicate as 'D' machomaddy Shell Programming and Scripting 7 01-28-2012 05:26 PM
join lines on line break in files mad_man12 Shell Programming and Scripting 6 07-31-2009 10:29 AM
remove all duplicate lines from all files in one folder lowmaster Shell Programming and Scripting 8 05-30-2009 07:45 AM
join on a file with multiple lines, fields crimper Shell Programming and Scripting 5 12-15-2008 03:50 AM



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