Issue
Let's say I have two large files. The first one is looking like this: File1:
N 4764
56.067000 50.667000 24.026000
HT1 4765
55.129000 51.012000 24.198000
HT2 4766
56.059000 50.183000 23.126000
and the second one: File2:
N NH2 -0.850000
HT1 H 0.222000
HT2 H 0.222000
I would like to replace all the N, HT1, and ..., in the first file with their matches in the second file (in second column of file2). so the outcome would be:
Outcome:
NH2 4764
56.067000 50.667000 24.026000
H 4765
55.129000 51.012000 24.198000
H 4766
56.059000 50.183000 23.126000
I am trying to do it with 'sed' but have not worked yet. Maybe awk is a better option?
*edit: My initial examples looked confusing so I changed my examples to the actual files I am dealing with. These are just three lines of my files.
Solution
One awk
idea:
awk '
FNR==NR { a[$1]=$2; next } # 1st file: save entries in array
$1 in a { $1=a[$1] } # 2nd file: if $1 is an index in array then replace $1 with match from array
1 # print current line
' File2 File1
This generates:
NH2 4764
56.067000 50.667000 24.026000
H 4765
55.129000 51.012000 24.198000
H 4766
56.059000 50.183000 23.126000
NOTE: assumes spacing does not need to be maintained in the lines undergoing a replacement
Answered By - markp-fuso Answer Checked By - Mary Flores (WPSolving Volunteer)