Search & Replace content of files using gsub in awk

Hi,I have 2 files master.txt & reference.txt as shown below & i require o/p as mentioned in file 3 using awk but content is not replacing properlymaster.txt:

919814047423728,Idea,PUN2372,MTS,AP25E4,Aircel,AP250A,Idea,AP2630,Airtel,PB919014002000,RCOM,AP919700099155,Aircel,APreference.txt2630,919700099155,123728,919700099155,225E4,250A,3919014002E40,919814047423728,10o/p:Airtel PB,Aircel AP,19198140474MTS AP,Aircel AP,2Aircel AP,Idea AP,3919014002E40,9198140474MTS AP

I want to match the exact length in reference.txt for replacement through master.txt & if it is not found then refernce file should print as it is. Please helpi have written the following sample code:

awk -F, 'NR==FNR {rep[$1]=$2" "$3; next}{ for (r in rep) gsub(r,rep[r],$0); print $0}' master.txt reference.txt

Please post your master file and reference file again using code tags.
From above it not clear.
And please post desired output also.

Regards,

pamu

Can you post sample input and expected output?

Not clear. What (fields, lines, characters) in reference.txt do you want to replace with what (see before)?
In your example, you replace every occurrence of master's field 1 in reference by master's field 2&3, which works fine.

would be more appropriate if you can share

template lines from both master.txt and the reference.txt and possibly if you know what the output should look like, a line with required pattern.

Master.txt2372,MTS,AP919848001104,Airtel,DL0819,MTS,MUM919849788001,Airtel,AP1430,Aircel MP,20Reference.txt2372,919848701430,46467919848002372,2372,471952372,919849788001,590270819,028803,10819,029801,10819,2372,1Output which is coming is as follows:MTS AP,91984870Aircel MP,4646791984800MTS AP,MTS AP,47195MTS AP,Airtel,AP59027MTS MUM,028803,1MTS MUM,029803,1MTS MUM,MTS AP,1Requirement:Only the exact match of each separator field should be replaced in reference text,i.e, output should look like as followsMTS AP,919848701430,46467919848002372,MTS AP,47195MTS AP,Airtel,AP59027MTS MUM,028803,1MTS MUM,029803,1MTS MUM,MTS AP,1

Can you use code tags.

Hi anbu,
Please let me know what exactly do you mean by code tag?
YOU MAY please check the same post again on below link
http://www.unix.com/shell-programming-scripting/218227-search-replace-content-using-awk-gsub.html\#post302780109

Master.txt

2372,MTS,AP
919848001104,Airtel,DL
0819,MTS,MUM
919849788001,Airtel,AP
1430,Aircel MP,20

Reference.txt

2372,919848701430,46467
919848002372,2372,47195
2372,919849788001,59027
0819,028803,1
0819,029801,1
0819,2372,1

Output which is coming is as follows:

MTS AP,91984870Aircel MP,46467
91984800MTS AP,MTS AP,47195
MTS AP,Airtel,AP59027
MTS MUM,028803,1
MTS MUM,029803,1
MTS MUM,MTS AP,1

Requirement:
Only the exact match of each separator field should be replaced in reference text,i.e, output should look like as follows

MTS AP,919848701430,46467
919848002372,MTS AP,47195
MTS AP,Airtel,AP59027
MTS MUM,028803,1
MTS MUM,029803,1
MTS MUM,MTS AP,1

Code is

awk -F, '
NR==FNR {rep[$1]=$2" "$3; next}
{ for (r in rep) gsub(r,rep[r],$0); print $0}' Master.txt Reference.txt

Please help in getting the code changed as per the required output for the exact match length

try

awk -F "," '{s=$1;sub($1",","");A=$0;next}{for(i=1;i<=NF;i++){P=P?P","A[$i]?A[$i]:$i}print P;P=""}' Master.txt Reference.txt

Not tested :smiley:

Try

awk     'NR==FNR        {MAS[$1]=$2" "$3; next}
                        {for (i=1; i<=NF; i++)
                           if ($i in MAS) $i=MAS[$i]
                        }
         1
        ' FS="," OFS="," Master.txt Reference.txt
MTS AP,919848701430,46467
919848002372,MTS AP,47195
MTS AP,Airtel AP,59027
MTS MUM,028803,1
MTS MUM,029801,1
MTS MUM,MTS AP,1

Thanks Rudi it did worked, but my requirement has changed a bit and need your help again as master file has changed

Master.txt

2372,MTS,AP
919848,Airtel,DL
0819,MTS,MUM
919849,Airtel,AP
1430,Aircel MP,20
405899136006473, MTS,DEL

Refernce.txt

2372,919848701430,46467
919848002372,2372,47195
2372,919849788001,59027
0819,028803,1
0819,029801,1
0819,2372,1405899136006473,TATA,MUM

Ouput should look like as follows:

MTS,AP,Airtel,DL,46467
Airtel,DL,MTS,AP,47195
MTS,AP,Airtel,AP,59027
MTS,MUM,028803,1
MTS,MUM,029801,1
MTS,MUM,MTS,AP,1

Using the below code, it is working fine in case master.txt has absolute value. But we need to match first 4 or 6 or 15 digits in reference.txt for each comma separated first 2 fields as master.txt will contain only these and not absolute values.

Use Code tags
You can edit your post later if you forget to add it.

Code tags as follows:

awk 'NR==FNR        {MAS[$1]=$2","$3; next}
                        {for (i=1; i<=2; i++)
                           if (substr($i,1,6) in MAS) $i=MAS[$i]
                           else if (substr($i,1,4) in MAS) $i=MAS[$i]
                           else if ($i in MAS) $i=MAS[$i]
                      
                        }
         1
        ' FS="," OFS="," Master.txt Reference.txt

Changed your code a bit

 awk 'NR==FNR        {MAS[$1]=$2","$3; next}
                        {for (i=1; i<=2; i++)
                           if (substr($i,1,6) in MAS) $i=MAS[substr($i,1,6)]
                           else if (substr($i,1,4) in MAS) $i=MAS[substr($i,1,4)]
                           else if ($i in MAS) $i=MAS[$i]
                      
                        }
         1
        ' FS="," OFS="," Master.txt Reference.txt
awk     'NR==FNR        {MAS[$1]=$2" "$3; next}
                        {for (i=1; i<=2; i++)
                           for (j in MAS) if (match($i, "^"j)) $i = MAS[j]
                        }
         1
        ' FS="," OFS="," Master.txt Reference.txt
1 Like

Thanks rudi it did worked. Can you please help again...
below is the link for the query raised