Replace values on file

Gents,

Please i need your help.

Using the file2.txt i will like to replace values in file3.txt.
Example in file 2 column 1 is the value to find in file3.txt and replace with value in colunm2 (file2.txt).

Example
file2.txt

21 1209
22 1210

file3.txt

SCI TB Timestamp Local : 8/30/17 7:52:52.980 AM
File # : 21
Is Raw : false
Shot # : 1752
Swath Name : 2567
Tape # : 3189
Tape Label : C4
________________________________________________________________________

SCI TB Timestamp Local : 8/30/17 7:51:52.980 AM
File # : 21
Is Raw : false
Shot # : 1752
Swath Name : 2568
Tape # : 3188
Tape Label : C4

The desired output should be (file4.txt)

SCI TB Timestamp Local : 8/30/17 7:52:52.980 AM
File # : 21
Is Raw : false
Shot # : 1752
Swath Name : 2567
Tape # : 3189
Tape Label : C4
________________________________________________________________________

SCI TB Timestamp Local : 8/30/17 7:51:52.980 AM
File # : 1209
Is Raw : false
Shot # : 1752
Swath Name : 2568
Tape # : 3188
Tape Label : C4

I use this code to fix this

awk 'NR==FNR{a[$1]=$2; next} /File # :/{gsub(/ /,"",$NF); $NF=" "a[$NF]}1' file2.txt FS=":" OFS=":" file3.txt > file4.txt

The problem I have is.. In file3.txt i have many File # : 21 which correspond to different record information.. Then what I like is to do the replacement ONLY where it is Tape # : 3188..

SCI TB Timestamp Local : 8/30/17 7:51:52.980 AM
File # : 1209
Is Raw : false
Shot # : 1752
Swath Name : 2568
Tape # : 3188
Tape Label : C4

Attached examples with more information and desired output file...

the original file is hug and contends a lot of data to replace, then i add small files with examples..

Appreciate your help :b:

You've got me - again and again and again. PLEASE make sure to remove DOS line teminators from text files before posting / attaching them here!
And, your results file is not consistent - not all matching records were modified. Try being more careful with data you post.
For your problem, try

awk '
BEGIN           {SRCH = "File # : "
                }
NR == FNR       {a[$1] = $2
                 next
                }
/Tape # : 3188/ {match ($0, SRCH "[0-9]*" )
                 T = substr ($0, RSTART+9, RLENGTH-9)
                 sub (SRCH  T, SRCH a[T])
                }
1
' /tmp/file2.txt RS=  ORS="\n\n" /tmp/file3.txt
1 Like

Hi RudiC,

It works perfectly.. Thanks a lot...