Join file with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join file with awk
# 1  
Old 07-07-2016
Join file with awk

I have a problem of joining 2 files with different position of key column

file1
Code:
INDIA|LEFT|123
USA|RIGHT|345
CHINA|LEFT|234

file2
Code:
123|HIGH
345|LOW

expected output
Code:
INDIA|LEFT|123|HIGH
USA|RIGHT|345|LOW
CHINA|LEFT|234|

based on column 3 in file 1 and column 2 in file 2, I want to print file1 plus its value from file2

I did this
Code:
awk -F"|" 'FILENAME=="file2"{A[$2]=$2} FILENAME=="file1"{if(A[$3]){print}}' file2 file1

but it keeps fail, lease any help
# 2  
Old 07-07-2016
Try
Code:
awk -F"|" 'FNR== NR {A[$1] = $2; next} {print $0, A[$3]}' OFS="|" file2 file1
INDIA|LEFT|123|HIGH
USA|RIGHT|345|LOW
CHINA|LEFT|234|

# 3  
Old 07-07-2016
Hello radius,

Could you please try following and let me know if this helps you(not tested though).
Code:
awk 'FNR==NR{A[$1]=$NF;next} ($NF in A){print $0 OFS A[$NF]}' FS=OFS="|" Input_file2 Input_file1

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. UNIX for Dummies Questions & Answers

Join with awk

I have file 1 (f1) 531902021342|610231416030043 531902021421|610231415853764 531902021460|610231406819593 531902021477|610231410151075 531902021481|610230208405255 531902021484|0 531902021557|610231407330625 531902021558|610230208106654 531902021564|610230204101407 file 2(f2)... (7 Replies)
Discussion started by: radius
7 Replies

3. UNIX for Dummies Questions & Answers

Join with awk using sprintf

Hi, Trying to join 2 files with awk (file1 has variable number of fields; file 2 has constant number of fields) file1: hook1|AA|BB|CC|DD hook2|EE|FF file2: hook1|11|22 hook2|33|44 hook3|55|66 output: hook1|11|22|AA|BB|CC|DD hook2|33|44|EE|FF hook3|55|66 What I tried so far:... (3 Replies)
Discussion started by: beca123456
3 Replies

4. UNIX for Dummies Questions & Answers

Join or Awk

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. I want to replace or join RBOSK-374 in file 2 with NB2781 FGH in file 1. Any help would be appreciated. a.log: RBOSK-374 AAA-B04-D16-K01 a.log: 0 XXX 602... (1 Reply)
Discussion started by: jimmyf
1 Replies

5. Shell Programming and Scripting

left join using awk

Hi guys, I need AWK to merge the following 2 files: file1 1 a 1 1 2 b 2 2 3 c 3 3 4 d 4 4 file2 a a/a c/c a/c c/c a/a c/t c c/t c/c a/t g/g c/c c/t desired output: 1 a 1 1 a/a c/c a/c c/c a/a c/t 2 b 2 2 x x x x x x 3 c 3 3 c/t c/c a/t g/g c/c c/t 4 d 4 4 x x x x x x (2 Replies)
Discussion started by: g1org1o
2 Replies

6. Shell Programming and Scripting

How to get awk to edit in place and join all lines in text file

Hi, I lack the utter fundamentals on how to craft an awk script. I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Discussion started by: n00ti
3 Replies

7. Shell Programming and Scripting

Join using awk

Hi - I want to join 2 files on matching keys ( column 1 in both files ) and check for unmatched columns, If for each record print the first unmatched column from both files and then proceed to next record and do the same. File 1: 123|Roy|jj|20/07/3000|25.48 125|Victor|kk|30/07/2009|34.56... (7 Replies)
Discussion started by: nbethy1
7 Replies

8. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

9. Shell Programming and Scripting

awk for join

Is it possible to do this with awk command?? sort1.txt a 10 b 20 sort2.txt a b c d join command join sort1.txt sort2.txt && join -v1 sort2.txt sort1.txt output a 10 b 20 (1 Reply)
Discussion started by: repinementer
1 Replies

10. Shell Programming and Scripting

File Join with different format using awk

File Join with different format using awk file1 file2 nawk -F"=" 'NR==FNR{a++;next} a{ print a, output Help is appreciated (5 Replies)
Discussion started by: pinnacle
5 Replies
Login or Register to Ask a Question