Haifeng Zhang
Haifeng Zhang

Reputation: 31885

Combine two files using awk, my command is not working

I have two files, each of them has 7 columns, I want to combine those two files based on the same column values.I used the following files to simulate my issue: File1 and file2 has one shared column, the keys. file1:

key1 abc
key2 def
key3 ghi

file2:

key1 hello-world
key2 amazing-time
key3 happy-day

Expected output:

key1 abc hello-world
key2 def amazing-time
key3 ghi happy-day

Here's my awk command:

awk -F'' 'NR==FNR {a[$1]=$2; next} ($1 in a){print $1, a[$1], $2}' file1 file2

My logic is:

  1. reads file1, store its first column as array(array a) index and store its second column as the corresponding array value

  2. processes file2, checks file2's first column is in array a's existing index or not, if it exists, print file2 's first column, file1's second column and file2's second column.

Unfortunately, it returns nothing, can anyone help me, what should I do to get my expected output, what is wrong in my awk command?

Upvotes: 0

Views: 115

Answers (1)

Kent
Kent

Reputation: 195039

remove the -F'' your codes should work.

Upvotes: 1

Related Questions