LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2006, 11:46 AM   #1
0aniel
LQ Newbie
 
Registered: Nov 2005
Posts: 19

Rep: Reputation: 0
Some help needed..


I have a problem with joining two files together.

File 1
123321 abc abc
124123 abc abc
125124 abc abc
120124 abc abc
127111 abc abc
128222 abc abc

File 2
123321 10
124123 20
125124 30
128222 40


The merged file Im trying to get should look as follows;

123321 abc abc 10
124123 abc abc 20
125124 abc abc 30
128222 abc abc 40
127111 abc abc
120124 abc abc

Any suggestion how I can do that using the "join" command?
 
Old 02-15-2006, 11:57 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Thumbs up

You can do:
join -a 1 -1 1 file1 -2 1 file2 |sort -k4

This will give you:
120124 abc abc
127111 abc abc
123321 abc abc 10
124123 abc abc 20
125124 abc abc 30
128222 abc abc 40

You can't make the ones without numbers appear at the bottom except by doing a reverse sort but if you do that then the numbers will be reverse as well (40, 30...).
 
Old 02-15-2006, 01:12 PM   #3
0aniel
LQ Newbie
 
Registered: Nov 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlightner
You can do:
join -a 1 -1 1 file1 -2 1 file2 |sort -k4
The above doesnt seem to be doing anything all i get is.

$ join -a 1 -1 1 file1 -2 1 file2 | sort -k4
usage: join [-a file_number | -v file_number] [-o list [-e string]]
[-t char] [-1 field] [-2 field] file1 file2

join [-a file_number] [-j field] [-j1 field] [-j2 field]
[-o list [-e string]] [-t char] file1 file2

Cheers
 
Old 02-15-2006, 02:08 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Odd.

I tested it on Debian Woody before posting. After your response I tested it also on RH EL AS 3 and it worked as typed.

The man page and the usage you got both say -a should be followed with the file but the message I got before hitting on above was complaining about invalid index given to -a so I'd changed that.

The usage message you get seems to suggest both file names should follow the rest of the line instead of the way it worked for me.

Try doing:
join -a 1 -1 1 -2 1 file1 file2 | sort -k4

If that doesn't work retry it using "-1" or "-2" instead "1" directly after the -a.

What distro are you running?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed shloog Linux - Newbie 2 05-21-2005 11:45 AM
help needed with QT sabeel_ansari Programming 1 01-21-2004 02:14 PM
Help Needed sandy Linux - Newbie 1 09-22-2003 02:54 AM
help needed trolsbane Linux - Hardware 3 09-06-2003 03:36 PM
Needed Help rajesh.s Linux - Hardware 0 12-17-2002 02:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration