×
UNIX.COM Login
Username:
Password:  
Show Password





×
UNIX.COM Login
Hello Unregistered
Welcome from United States
You are somewhere around Ashburn
Last Visit: 1 Minute Ago @ 03:27 PM
Bits: 0
UNIX Time: 1531682838

👤


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

 

 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-11-2012   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
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 10-11-2012   -   Original Discussion by jimmyf
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 July 2018, 1:06 PM EDT
Location: Saskatchewan
Posts: 22,722
Thanks: 1,192
Thanked 4,347 Times in 4,003 Posts
What output would you want for that input?
Sponsored Links
    #3  
Old 10-11-2012   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
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 10-11-2012   -   Original Discussion by jimmyf
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 July 2018, 1:06 PM EDT
Location: Saskatchewan
Posts: 22,722
Thanks: 1,192
Thanked 4,347 Times in 4,003 Posts


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

Sponsored Links
    #5  
Old 10-11-2012   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
Thanked 2 Times in 1 Post
syntax error Corona
Sponsored Links
    #6  
Old 10-11-2012   -   Original Discussion by jimmyf
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 July 2018, 1:06 PM EDT
Location: Saskatchewan
Posts: 22,722
Thanks: 1,192
Thanked 4,347 Times in 4,003 Posts
Which syntax error?

Use nawk on solaris.
Sponsored Links
    #7  
Old 10-11-2012   -   Original Discussion by jimmyf
hexram's Unix or Linux Image
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

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 04:15 PM
Join lines with the same start string andrejm Shell Programming and Scripting 10 10-02-2011 02:26 PM
Join 2 files with multiple columns: awk/grep/join? InfoSeeker UNIX for Dummies Questions & Answers 3 12-01-2009 07:45 PM
sql,multiple join,outer join issue robbiezr Programming 0 06-05-2009 11:26 PM
Use of uninitialized value in join or string at arraydef.pl praveen_b744 Shell Programming and Scripting 5 08-31-2007 10:09 AM



All times are GMT -4. The time now is 03:27 PM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.