-1

I have two files with FILE1 containing lots of lines and FILE2 with KEY VALUE parms. I need to compare FILE2 with FILE1 and if there is match the corresponding word in FILE1 should be replace with next column in FILE2.

Example:

FILE1:

<SOME YAML CODE
-------------->
PARM1
PARM2
PARM3
PARM4
<END OF YAML CODE
---------------->

FILE2:

PARM1 mmddyy
PARM2 hhmmss
PARM3 awsid
PARM4 cc

So for every match from FILE2 in FILE1, the corresponding word in FILE1 should be replace with 2nd column in FILE2. So the desired output should like:

<SOME YAML CODE
-------------->
mmddyy
hhmmss
awsid
cc
<END OF YAML CODE
---------------->

I tried using sed with limited knowledge but not achieving the desired output.

Appreciate your time and support

terdon
  • 242,166
Ram
  • 335

1 Answers1

0

You can use awk:

awk '
    NR==FNR{k[$1]=$2;}
    NR!=FNR{if($1 in k){$0=k[$1]};print}
' file2 file1

Save an array of fields from file2 and if found in file1, replace the line ($0) with the content of the corresponding array.

pLumo
  • 22,565
  • You are replacing the entire line here though, not just the word. This time I did read the question! :) "for every match from FILE2 in FILE1, the corresponding word in FILE1 should be replace with 2nd column in FILE2" – terdon Sep 17 '21 at 15:42
  • might be, but then this is a very bad example text.... – pLumo Sep 17 '21 at 15:50
  • pLumo-- you are genious and saved my day. This worked like charm, appreciate your help. Thankyou – Ram Sep 17 '21 at 17:06
  • just realized that FILE1 has some lines like _PARM1, and the desired output should be

    _mmddyy

    Any idea on this please

    – Ram Sep 17 '21 at 17:31