Multi column join


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multi column join
# 1  
Old 07-17-2014
Multi column join

Hello folks, having a new problem with an old solution I previously had working, but think i'm missing something here.

File1:
Code:
900001093|HAMU1|SUDO_ALIAS
100100361|IAM_IDS|SUDO_ALIAS
100100361|AMPF|SUDO_ALIAS

File2:
Code:
ABC123456|Cust1|900001093|myemail@here.com|Jane Smith|Win1|hamu1
DEF234567|Cust2|100100361|hisemail@there.com|John Doe|UX1|iam_ids
DEF234567|Cust2|100100361|hisemail@there.com|John Doe|UX1|ampf

This is my code I am trying to use:
Code:
awk -F\| 'FNR==NR {(a[$1] = $3) && (a[$2] = toupper($7));next} {print a[$1,$2,$3], $4, $5}' File1 File2

What I am trying to do is match fields 1,2 of File1 against fields 3,7 of File2, and print all of File1 fields, and fields 4,5 of File2 only. However, what I am getting is only fields 4,5 out of File2 with space as a field separator.

Expected output:
Code:
900001093|HAMU1|SUDO_ALIAS|myemail@here.com|Jane Smith
100100361|IAM_IDS|SUDO_ALIAS|hisemail@there.com|John Doe
100100361|AMPF|SUDO_ALIAS |hisemail@there.com|John Doe

# 2  
Old 07-17-2014
Big problem #1: You are using "field 7" in the 'NR==FNR' code block which gets run for file 1 only -- that file has no "field 7". All that code block should be doing is loading file 1, it knows nothing about file 2 yet.

The other code block deals with the contents of file 2. What it's supposed to do is retrieve the matching contents of the array and print it all like you wanted.

Big problem #2: You are assigning A[$1], then assigning A[$2], then expecting output in A[$1,$2,$3]. Doesn't work that way. If you set A["stuff"] to 3, then A["else"] to 4, A["stuff","else"] won't get you anything sensible.

This is because "stuff","else" just becomes a string -- "stuff" SUBSEP "else", where SUBSEP is a special variable which is usually some odd nonprinting ASCII character. This lets awk pretend to have multidimensional arrays while actually just keeping a single index.

So:

Code:
$ awk -F"|" -v OFS="|" 'NR==FNR { A[$1,$2]=$3; next } # Save data

$3 SUBSEP toupper($7) in A { # If the data is found in the array
        print $3, toupper($7), A[$3 SUBSEP toupper($7)], $4, $5;
}' file1 file2

900001093|HAMU1|SUDO_ALIAS|myemail@here.com|Jane Smith
100100361|IAM_IDS|SUDO_ALIAS|hisemail@there.com|John Doe
100100361|AMPF|SUDO_ALIAS|hisemail@there.com|John Doe

$

Oh, and -v OFS="|" controlls what print "A","B","C" does. By default it would print "A B C". By setting it to | it prints "A|B|C".
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-17-2014
Thanks Corona688. That solved my problem. And now that I see it in the code, the problem is a bit more obvious. Appreciate the explanation.
This User Gave Thanks to dagamier For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Join with awk different column

hi guys, i need help I need to join file2 to file1 when column 3 in my file1 and column 1 in my file2 in the same string file1 AA|RR|ESKIM RE|DD|RED WE|WW|SUPSS file2 ESKIM|ES SUPSS|SS Output AA|RR|ESKIM|ES RE|DD|RED| WE|WW|SUPSS|SS (3 Replies)
Discussion started by: radius
3 Replies

2. Programming

Update a column from a Join

Here is my select that I have to identify the child records that are Open (e.c7 < 6000) when the parent (t2068) c.c7 > 3 SELECT c.c1000000161, c.c7, c.c1000000019, e.c1000000829 FROM t2068 c INNER JOIN t1533 e ON e.c1000000829 = c.c301572100 where c.c7 > 3... (2 Replies)
Discussion started by: newborndba
2 Replies

3. UNIX for Dummies Questions & Answers

Join files by second column

I have file input file1 1/1/2013 A 553.0763397 96 16582 1/1/2013 B 459.8333588 195 11992 1/2/2013 A 844.2973022 306 19555 1/2/2013 B 833.9300537 457 20165 1/3/2013 A 563.6917419 396 13879 1/3/2013 B 632.0749969 169 ... (1 Reply)
Discussion started by: radius
1 Replies

4. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

5. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Shell Programming and Scripting

join two column

Hi I want to join last two column: File A U3268 2689 61 12 10 U3268 2684 71 13 0 U3268 2685 81 13 1 Output: U3268 2689 61 12/10 U3268 2684 71 13/0 U3268 2685 81 13/1 Thanks (6 Replies)
Discussion started by: pareshkp
6 Replies

7. Shell Programming and Scripting

Sort the multi column rows

abc xyz - - - - - - - - - - - How to sort the second column in ascending order. (2 Replies)
Discussion started by: sandy1028
2 Replies

8. UNIX for Dummies Questions & Answers

Join 2 files using first column

Hi, I'm trying to compare the first column of two files (tab or whitespace delimited, either way's fine, I`ve got both) and print the lines that are identical for the first column of both files. Something like this: File1 AAA 26 49 7 27 36 33 46 75 73 69 AAAAA 4 10 4 7 10 18 21... (2 Replies)
Discussion started by: vanesa1230
2 Replies

9. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

10. Shell Programming and Scripting

join cols from multi files into one file

Hi Fields in Files 1,2,3,4 are pipe"|" separated. Say I want to grep col1 from File1 col3 from File2 col4 from File3 and print to File4 in the following order: col3|col1|col4 what is the best way of doing this? Thanks (2 Replies)
Discussion started by: vbshuru
2 Replies
Login or Register to Ask a Question