LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-03-2011, 10:50 PM   #1
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Rep: Reputation: 0
Question adding line from file1 into a line of another file based on maching IDs


Hello

i wish to add information to one of my files based on matching IDs, here is an example

(the id is the 3 colunm)
File1.txt
v0001 bird 45
v0002 bird 47
v0003 cat 50
v0004 dog 56
v0005 dog 78

(the ID is the 2 colunm)
File2.txt
bird 45 age_7
cat 50 age_6
dog 56 age_5

and the output i wish to be

OUTPUT:
v0001 bird 45 age_7
v0002 bird 47
v0003 cat 50 age_6
v0004 dog 56 age_5
v0005 dog 78

so as you can see the ones that do not match are still present, and the ones that do match just have the extra information from file2.txt added to them.

i thought about using join but that only seems to join the ones that match displays thoes only. i would like all the information in the output file
please help! thank you all
 
Old 01-03-2011, 10:57 PM   #2
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
PS. this is what command line i used

join -1 3 -2 2 file1.txt file2.txt

but as i said, it only displays the matching lines...
 
Old 01-03-2011, 10:58 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Is it likely you can have the same ID but with a different animal? ie can you have dog 50 in either file?
 
Old 01-03-2011, 11:18 PM   #4
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Hi grail

Yes you can have a dog with a id of 50 and a cat with a ID of 50, but no 2 dogs will have the same ID. (in reality these files are organised so that all dogs are in a dog file and all cats are in a cat file etc.

example
dog.txt
v0004 dog 56
v0005 dog 78
v0006 dog 55

but the files are matched based on dog/cat/bird and there ID's (56, 78, 55 etc)
 
Old 01-03-2011, 11:24 PM   #5
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
omg i forgot to mention i am programming in bash!
 
Old 01-04-2011, 02:48 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well awk seems to be well suited to this issue, something like:
Code:
awk 'FNR==NR{_[$1$2]=$3;next}$2$3 in _{$0=$0" "_[$2$3]}1' File2.txt File1.txt
 
Old 01-06-2011, 01:06 AM   #7
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks heaps man!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
merge file1 at end of line file2 porkcharsui Linux - Newbie 2 03-23-2010 06:27 AM
/file1: line 8: [: too many arguments sudheerkappala Linux - General 3 10-22-2008 09:01 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM


All times are GMT -5. The time now is 06:39 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration