Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Thanks Jaypal .. its working .. thank you very much –  Timson Feb 18 at 6:15
    
If possible can you explain the logic –  Timson Feb 18 at 7:21
    
@Timson I have added comments to explain the logic. Hope that helps! –  jaypal Feb 20 at 3:24
    
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 Feb 27 at 16:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.