Home Man
Search
Today's Posts
Register

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

Tags
duplicate, fields, join, shell scripts, unix

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 12-12-2012
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
# 2  
Old 12-12-2012
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

# 3  
Old 12-12-2012
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

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Join files on multiple fields sheetalk Shell Programming and Scripting 6 07-29-2015 07:35 AM
Remove duplicate lines from file based on fields Lord Spectre Shell Programming and Scripting 2 11-26-2014 08:59 AM
Remove duplicate lines from text files. pasc Windows & DOS: Issues & Discussions 1 07-29-2014 09:31 AM
Join lines from two files based on match pathunkathunk Shell Programming and Scripting 2 08-18-2013 03:55 PM
Join fields comparing 4 fields using awk aksijain Shell Programming and Scripting 1 04-12-2013 02:59 AM
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 05:24 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password