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
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