Sponsored Content
Top Forums Shell Programming and Scripting how to join two files with awk. Post 302593370 by ken002 on Thursday 26th of January 2012 05:03:40 PM
Old 01-26-2012
Quote:
Originally Posted by bartus11
This:
Code:
awk 'NR==FNR {a[$1]=$1; next} {if ($1 in a);  print $1, a[$1]}' file1 file2

is equivalent to this:
Code:
awk 'NR==FNR {a[$1]=$1; next} {if ($1 in a) {};  print $1, a[$1]}' file1 file2

I hope you get my point Smilie
Maybe I got it, but I still confused something.
Code:
if ($1 in a) {};

means it prints the matched records in file2 then
Code:
print $1, a[$1]}

print out all records in files 1 and file2. if this is true. the matched records in file2 should be duplicated, but the output doesn't contains any duplicates.

Thanks again.

---------- Post updated at 05:03 PM ---------- Previous update was at 04:49 PM ----------

Quote:
Originally Posted by bartus11
This:
Code:
awk 'NR==FNR {a[$1]=$1; next} {if ($1 in a);  print $1, a[$1]}' file1 file2

is equivalent to this:
Code:
awk 'NR==FNR {a[$1]=$1; next} {if ($1 in a) {};  print $1, a[$1]}' file1 file2

I hope you get my point Smilie
I got it
Code:
if ($1 in a) {}

means if match found, do nothing.

---------- Post updated at 05:03 PM ---------- Previous update was at 05:03 PM ----------

Quote:
Originally Posted by bartus11
This:
Code:
awk 'NR==FNR {a[$1]=$1; next} {if ($1 in a);  print $1, a[$1]}' file1 file2

is equivalent to this:
Code:
awk 'NR==FNR {a[$1]=$1; next} {if ($1 in a) {};  print $1, a[$1]}' file1 file2

I hope you get my point Smilie
I got it
Code:
if ($1 in a) {}

means if match found, do nothing.
ken002
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Left join on files using awk

nawk 'NR==FNR{a;next} {if($1 in a) print $1,"Found" else print}' OFS="," File_B File_A The above code is not working help is appreciated (6 Replies)
Discussion started by: pinnacle
6 Replies

2. 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

3. Shell Programming and Scripting

Join multiple files by column with awk

Hi all, I searched through the forum but i can't manage to find a solution. I need to join a set of files placed in a directory (~1600) by column, and obtain an output with first and second column common to each file, but following columns are taken from the file in the list (precisely the fourth... (10 Replies)
Discussion started by: macsx82
10 Replies

4. UNIX for Dummies Questions & Answers

how to join two files using "Join" command with one common field in this problem?

file1: Toronto:12439755:1076359:July 1, 1867:6 Quebec City:7560592:1542056:July 1, 1867:5 Halifax:938134:55284:July 1, 1867:4 Fredericton:751400:72908:July 1, 1867:3 Winnipeg:1170300:647797:July 15, 1870:7 Victoria:4168123:944735:July 20, 1871:10 Charlottetown:137900:5660:July 1, 1873:2... (2 Replies)
Discussion started by: mindfreak
2 Replies

5. Shell Programming and Scripting

Awk - join multiple files

Is it possible to join all the files with input1 based on 1st column? input1 a b c d e f input2 a b input3 a e input4 c (2 Replies)
Discussion started by: quincyjones
2 Replies

6. 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

7. Shell Programming and Scripting

awk program to join 2 fields of different files

Hello Friends, I just need a small help, I need an awk program which can join 2 fields of different files which are having one common field into one file. File - 1 FileName~Size File- 2 FileName~Date I need the output file in the following way O/P- File FileName~Date~Size For... (4 Replies)
Discussion started by: abhisheksunkari
4 Replies

8. Shell Programming and Scripting

awk join 2 files

Hello All, file1 A1;B1;C1;D1;E1;F1;G1;H1;III1;J1 A2;B2;C2;D2;E2;F2;G2;H2;III2;J2 A3;B3;C3;D3;E3;F3;G3;H3;III3;J3 A4;B4;C4;D4;E4;F4;G4;H4;III4;J4file2 III1 ZZ1 S1 Y 1 P1 None NA III2 ZZ2 S2 Y 3 P2 None NA III3 ZZ3 S2 Y 5 ... (2 Replies)
Discussion started by: vikus
2 Replies

9. Shell Programming and Scripting

Join two files using awk

Hello All; I have two files: File1: abc def pqr File2: abc,123 mno,456 def,989 pqr,787 ghj,678 (6 Replies)
Discussion started by: mystition
6 Replies

10. 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
All times are GMT -4. The time now is 04:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy