Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

join - using *only* tab as a field separator

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-15-2009   -   Original Discussion by FrancoisCN
FrancoisCN's Unix or Linux Image
FrancoisCN FrancoisCN is offline
Registered User
 
Join Date: May 2009
Last Activity: 3 August 2009, 2:35 PM EDT
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
join - using *only* tab as a field separator

I have files with tabs separating the fields but those fields values can have whitespace characters (basically a text string). I want to instruct join to not consider white spaces as separators but only tabs.


I have tried:
join -t "<Tab>" file1 file2
join -t "<tab>" file1 file2
join -t "\t" file1 file2
Sponsored Links
    #2  
Old Unix and Linux 06-15-2009   -   Original Discussion by FrancoisCN
ahmad.diab's Unix or Linux Image
ahmad.diab ahmad.diab is offline
Registered User
 
Join Date: May 2008
Last Activity: 7 August 2017, 4:42 AM EDT
Location: Amman, Jordan
Posts: 645
Thanks: 2
Thanked 19 Times in 19 Posts
kindly submit some input texts and the desired output text
BR
Sponsored Links
    #3  
Old Unix and Linux 06-15-2009   -   Original Discussion by FrancoisCN
FrancoisCN's Unix or Linux Image
FrancoisCN FrancoisCN is offline
Registered User
 
Join Date: May 2009
Last Activity: 3 August 2009, 2:35 PM EDT
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
can't do tabs on forums but here:

file1
abc def <Tab> X1
ghi jkl <Tab> X2

file2
abc def <Tab> Y1
ghi jkl <Tab> Y2

output
abc def <Tab> X1 <Tab> Y1
ghi jkl <Tab> X2 <Tab> Y2
    #4  
Old Unix and Linux 06-15-2009   -   Original Discussion by FrancoisCN
ahmad.diab's Unix or Linux Image
ahmad.diab ahmad.diab is offline
Registered User
 
Join Date: May 2008
Last Activity: 7 August 2017, 4:42 AM EDT
Location: Amman, Jordan
Posts: 645
Thanks: 2
Thanked 19 Times in 19 Posts

Code:
use this code:-

nawk  '
NR==FNR {
a[$1$2]=$3 ; next
}

{ print s=( $1$2 in a ) ? $1FS$2FS"\t"FS$3FS"\t"FS a[$1$2] :  $0 RS }

'  file2 file1

note:- if you don't have nawk use /usr/xpg4/bin/awk

BR


Last edited by ahmad.diab; 06-15-2009 at 01:28 PM..
Sponsored Links
    #5  
Old Unix and Linux 06-15-2009   -   Original Discussion by FrancoisCN
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 14 December 2017, 6:45 PM EST
Location: Foxborough, MA
Posts: 8,203
Thanks: 231
Thanked 785 Times in 732 Posts
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Sponsored Links
    #6  
Old Unix and Linux 06-15-2009   -   Original Discussion by FrancoisCN
ahmad.diab's Unix or Linux Image
ahmad.diab ahmad.diab is offline
Registered User
 
Join Date: May 2008
Last Activity: 7 August 2017, 4:42 AM EDT
Location: Amman, Jordan
Posts: 645
Thanks: 2
Thanked 19 Times in 19 Posts

Code:
thanks Mr. vgersh99 for the advice..hope you can understand the code now after modification :)

Sponsored Links
    #7  
Old Unix and Linux 06-15-2009   -   Original Discussion by FrancoisCN
FrancoisCN's Unix or Linux Image
FrancoisCN FrancoisCN is offline
Registered User
 
Join Date: May 2009
Last Activity: 3 August 2009, 2:35 PM EDT
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
It seems to me the solution you propose would only work if there is only 1 space in the first expression but wouldn't if it had multiple (ie "a b cdef"). Anyways, I tried a solution of my own, which isn't that pretty, but works.


1. substitute spaces to @@@
2. substitute tab to space
3. join
4. revert space to tab
5. revert @@@ to space


Code:
sed -e 's/ /@@@/g' ./temp/file1 | sed -e 's/<tab>/ /g' > ./temp/file1.rdy2join

sed -e 's/ /@@@/g' ./temp/file2 | sed -e 's/<tab>/ /g' > ./temp/file2.rdy2join

join -t " " -1 1 -2 1 ./temp/file1.rdy2join ./temp/file2.rdy2join > ./temp/output

then I reverted with sed again.

Sponsored Links
Closed

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
Can't figure out what field separator to use in awk.... thom.mattson UNIX for Dummies Questions & Answers 3 06-10-2009 03:42 AM
field separator in Perl ahsog Shell Programming and Scripting 38 04-17-2009 09:30 PM
How to set Field Separator for TCLSH??? :S laptop87 Shell Programming and Scripting 2 12-19-2008 11:07 AM
Field separator Ques. yahyaaa Shell Programming and Scripting 6 09-16-2008 02:54 PM
How do I specify tab as field separator for sort? SSteve UNIX for Dummies Questions & Answers 8 04-26-2005 05:39 PM



All times are GMT -4. The time now is 08:51 PM.