Join On A String | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Join On A String

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-11-2012
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 9 October 2014, 12:05 PM EDT
Posts: 66
Thanks: 32
Thanked 0 Times in 0 Posts
Join On A String

Is is possible to join on unsorted files by a string? Don't want to sort because there is other text that is already in a good format.

File1 has this text string:
ABCD-123 FGH

File2 has this text string:
ABCD-123

I want to replace or join ABCD-123 in file 2 with ABCD-123 FGH in file 1.
Thank you.
Sponsored Links
    #2  
Old 10-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 November 2014, 5:04 PM EST
Location: Saskatchewan
Posts: 19,943
Thanks: 848
Thanked 3,420 Times in 3,205 Posts
What output would you want for that input?
Sponsored Links
    #3  
Old 10-11-2012
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 9 October 2014, 12:05 PM EDT
Posts: 66
Thanks: 32
Thanked 0 Times in 0 Posts
Desired output would be for file2: ABCD-123 FGH. There are multiple ones though. So it would be:
ABCD-123 FGH and BCDE-345 JKL etc.
Not using sed because each instance is unique. Not sure if I'm being clear.

---------- Post updated at 03:24 PM ---------- Previous update was at 03:17 PM ----------

Just looked at my response, let me re-phrase it.
Desired output would be
ABCD-123 AAA
ABCD-456 BBB
ABCD-789 CCC
But these would appear in different places in the text file. Hope this is clearer and I do appreciate the help.
    #4  
Old 10-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 November 2014, 5:04 PM EST
Location: Saskatchewan
Posts: 19,943
Thanks: 848
Thanked 3,420 Times in 3,205 Posts

Code:
awk 'NR==FNR { A[$1]=1; next } $1 in A' file2 file1

Sponsored Links
    #5  
Old 10-11-2012
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 9 October 2014, 12:05 PM EDT
Posts: 66
Thanks: 32
Thanked 0 Times in 0 Posts
syntax error Corona
Sponsored Links
    #6  
Old 10-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 November 2014, 5:04 PM EST
Location: Saskatchewan
Posts: 19,943
Thanks: 848
Thanked 3,420 Times in 3,205 Posts
Which syntax error?

Use nawk on solaris.
Sponsored Links
    #7  
Old 10-11-2012
hexram hexram is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 13 April 2014, 10:20 PM EDT
Posts: 20
Thanks: 5
Thanked 3 Times in 3 Posts
Maybe it should be:

Code:
awk 'NR==FNR { A[$1]=$1; next } $1 in A' file2 file1
                     ^
                  Missing

Sorry, just read the post and thought "this is the error"; just did not test it yet.
(I HATE variable spacing fonts! Had to edit this post too many times just to align the arrow with the error)

Moderator's Comments:
Nope. The code is fine.
Sponsored Links
Closed 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
How to use the the join command to join multiple files by a common column evelibertine UNIX for Dummies Questions & Answers 5 07-05-2012 05:15 PM
Join lines with the same start string andrejm Shell Programming and Scripting 10 10-02-2011 03:26 PM
Join 2 files with multiple columns: awk/grep/join? InfoSeeker UNIX for Dummies Questions & Answers 3 12-01-2009 08:45 PM
sql,multiple join,outer join issue robbiezr Programming 0 06-06-2009 12:26 AM
Use of uninitialized value in join or string at arraydef.pl praveen_b744 Shell Programming and Scripting 5 08-31-2007 11:09 AM



All times are GMT -4. The time now is 02:38 AM.