Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Join On A String

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-11-2012
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 8 September 2016, 4:00 PM EDT
Posts: 80
Thanks: 40
Thanked 2 Times in 1 Post
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 Unix and Linux 10-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 December 2016, 5:18 PM EST
Location: Saskatchewan
Posts: 21,658
Thanks: 1,008
Thanked 3,980 Times in 3,697 Posts
What output would you want for that input?
Sponsored Links
    #3  
Old Unix and Linux 10-11-2012
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 8 September 2016, 4:00 PM EDT
Posts: 80
Thanks: 40
Thanked 2 Times in 1 Post
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 Unix and Linux 10-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 December 2016, 5:18 PM EST
Location: Saskatchewan
Posts: 21,658
Thanks: 1,008
Thanked 3,980 Times in 3,697 Posts

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

Sponsored Links
    #5  
Old Unix and Linux 10-11-2012
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 8 September 2016, 4:00 PM EDT
Posts: 80
Thanks: 40
Thanked 2 Times in 1 Post
syntax error Corona
Sponsored Links
    #6  
Old Unix and Linux 10-11-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 December 2016, 5:18 PM EST
Location: Saskatchewan
Posts: 21,658
Thanks: 1,008
Thanked 3,980 Times in 3,697 Posts
Which syntax error?

Use nawk on solaris.
Sponsored Links
    #7  
Old Unix and Linux 10-11-2012
hexram hexram is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 9 August 2015, 11:42 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:
Join On A String Nope. The code is fine.
Sponsored Links
 

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
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:00 AM.