Search & Replace content of files using gsub in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search & Replace content of files using gsub in awk
# 8  
Old 03-14-2013
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
https://www.unix.com/shell-programmin...#post302780109

Master.txt
Code:
2372,MTS,AP
919848001104,Airtel,DL
0819,MTS,MUM
919849788001,Airtel,AP
1430,Aircel MP,20

Reference.txt
Code:
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:
Code:
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

Code:
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
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 help in getting the code changed as per the required output for the exact match length

Last edited by Franklin52; 03-14-2013 at 04:23 AM.. Reason: Please use code tags for data and code samples
# 9  
Old 03-14-2013
try

Code:
awk -F "," '{s=$1;sub($1",","");A[s]=$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 Smilie
# 10  
Old 03-14-2013
Try
Code:
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

# 11  
Old 03-19-2013
Thanks Rudi it did worked, but my requirement has changed a bit and need your help again as master file has changed

Master.txt
Code:
2372,MTS,AP
919848,Airtel,DL
0819,MTS,MUM
919849,Airtel,AP
1430,Aircel MP,20
405899136006473, MTS,DEL

Refernce.txt
Code:
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:
Code:
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.

Last edited by Franklin52; 03-19-2013 at 04:17 AM.. Reason: Please use code tags for data and code samples
# 12  
Old 03-19-2013
Use Code tags
You can edit your post later if you forget to add it.
# 13  
Old 03-19-2013
Code tags as follows:

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


Last edited by Scrutinizer; 03-19-2013 at 07:06 AM.. Reason: Added code tags
# 14  
Old 03-19-2013
Changed your code a bit

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies

2. Shell Programming and Scripting

Replace characters in string with awk gsub

Hi I have a source file that looks like a,b,c,d,e,f,g,h,t,DISTI(USD),MSRP(USD),DIST(EUR),MSRP(EUR),EMEA-DISTI(USD),EMEA-MSRP(USD),GLOBAl-DISTI(USD),GLOBAL-MSRP(USD),DISTI(GBP), MSRP(GBP) I want to basically change MSRP(USD) to MSRP,USD and DIST(EUR) to DIST,EUR and likewise for all i'm using... (3 Replies)
Discussion started by: r_t_1601
3 Replies

3. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

4. Shell Programming and Scripting

Search & replace content using awk/gsub

Hi, I have two files master.txt & reference.txt. Sample below 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... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

8. Shell Programming and Scripting

awk and gsub - how to replace only the first X occurrences

I have a text (text.txt) and I would like to replace only the first 2 occurrences of a word (but I might need to replace more): For example, if text is this: CAR sweet head hat red yellow CAR book brown tiger CAR cow CAR CAR milk I would like to replace the word "CAR" with word... (12 Replies)
Discussion started by: bingel
12 Replies

9. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

10. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies
Login or Register to Ask a Question