How to replace an exact matching word using gsub

0

Please see the below script - On this

nawk 'NR==FNR { a[$1]=$2 ; next} {for ( i in a) gsub(i,a[i])}1' file.dat 1.txt

1.txt

ioiufeioru
dfoiduf
MO_CIF_INP438
fjkdj MO_CIF_INP
dsjhdf BP_LINKED_TETES
dehdueuh MO_INP_BPRESP

file.dat

MO_CIF_INP TRO_MO_CIF_INP

BP_LINKED_TETES TRO_BP_LINKED_TETES

MO_BPID TRO_MO_BPID

MO_INP_BPRESP TRO_MO_INP_BPRESP

and my output for the above script is

ioiufeioru
dfoiduf
TRO_MO_CIF_INP438
fjkdj TRO_MO_CIF_INP
dsjhdf TRO_BP_LINKED_TETES
dehdueuh TRO_MO_INP_BPRESP

My intention is to replace only the matching word not all in the above case MO_CIF_INP438 is also getting replaced. How we can use word search? I tried below case but not working

1.

nawk 'NR==FNR { a[$1]=$2 ; next} {for ( i in a) gsub(/i/,a[i])}1' file.dat 1.txt


TRO_BP_LINKED_TETESoTRO_BP_LINKED_TETESufeTRO_BP_LINKED_TETESoru
dfoTRO_BP_LINKED_TETESduf
MO_CIF_INP438
fjkdj MO_CIF_INP
dsjhdf BP_LINKED_TETES
dehdueuh MO_INP_BPRESP

2.

nawk 'NR==FNR { a[$1]=$2 ; next} {for ( i in a) gsub(\<i\>,a[i])}1' file.dat 1.txt
nawk: syntax error at source line 1
 context is
        NR==FNR { a[$1]=$2 ; next} {for ( i in a) >>>  gsub(\ <<< <i\>,a[i])}1
nawk: illegal statement at source line 1

Timson

Posted 2014-02-13T16:43:43.067

Reputation: 1

Answers

2

You can try something like:

awk '

# Read entire file.dat in an array indexed at column1 having value of column2

NR==FNR { 
    a[$1]=$2; 

# Skip the next action statements until file.dat is completely stored

    next 
}

# For each index element of array

{
    for(i in a) { 

# Iterate over each values of line from 1.txt file

        for(x=1;x<=NF;x++) {

# If an exact match is found replace it with array element else leave it as is. 

            $x=(i==$x)?a[i]:$x
            }
        }
}1' file.dat 1.txt

$ head file.dat 1.txt 
==> file.dat <==
MO_CIF_INP TRO_MO_CIF_INP

BP_LINKED_TETES TRO_BP_LINKED_TETES

MO_BPID TRO_MO_BPID

MO_INP_BPRESP TRO_MO_INP_BPRESP

==> 1.txt <==
ioiufeioru
dfoiduf
MO_CIF_INP438
fjkdj MO_CIF_INP
dsjhdf BP_LINKED_TETES
dehdueuh MO_INP_BPRESP

$ awk '              
  NR==FNR { 
      a[$1]=$2; 
      next 
  }
  {for(i in a) { 
      for(x=1;x<=NF;x++) {
          $x=(i==$x)?a[i]:$x
          }
      }
  }1' file.dat 1.txt
ioiufeioru
dfoiduf
MO_CIF_INP438
fjkdj TRO_MO_CIF_INP
dsjhdf TRO_BP_LINKED_TETES
dehdueuh TRO_MO_INP_BPRESP

jaypal singh

Posted 2014-02-13T16:43:43.067

Reputation: 2 134

If possible can you explain the logic – Timson – 2014-02-18T07:21:22.650

@Timson I have added comments to explain the logic. Hope that helps! – jaypal singh – 2014-02-20T03:24:04.593

I got one more issue in that ,in my file the word separation can be white spaces or "," or it can be ":" for example if my file a.txt contains a pattern like - hjdfgjf,BP_LINKED_TETES,fkdsjfj:BP_LINKED_TETES jdfhdj has to be replaced like hjdfgjf,TRO_BP_LINKED_TETES,fkdsjfj:TRO_BP_LINKED_TETES jdfhdj , currently that is not replacing. Can you please help me once again – Timson – 2014-02-27T16:09:53.367