jecha
jecha

Reputation: 33

Use Awk to get data from two files

I have two different files with two columns each.

file1.txt

DevId   Group
aaa     A
bbb     B

file2.txt

Group   RefId
A       111-222-333
B       444-555-666

I need only need DevId and its corresponding RefId.

Required Output

DevId   RefId
aaa     111-222-333
bbb     444-555-666

I tried using this syntax but I can't get it correctly.

awk -F, -v OFS=, 'NR==FNR{a[$1]=$2;next}{print a[$2],$1}' file2.txt file1.txt

I hope someone could help me.

Upvotes: 2

Views: 157

Answers (1)

ssemilla
ssemilla

Reputation: 3970

Here:

awk -v RS="\r\n" 'FNR==NR{a[$1]=$2;next}{ print $1, a[$2]}' file2.txt file1.txt

This was modified from Awk multiple files which I suggest you read for the explanation.

Edit: As mentioned by @JamesBrown, added -v RS="\r\n" for line endings

Upvotes: 2

Related Questions