Replace from reference file awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace from reference file awk
# 1  
Old 09-21-2013
Replace from reference file awk

Basically want to replace any field in input file from the refernce file ...
for example.
clar_2400:3113 in input file will be replaced by clar_2400:3113[2323]
Input file field seperator is ","
Field which is not found in reference will stay as it is ...


Input File
Code:
 
clar_2400:3113,,,[MIG_RLP_3113],40.000,[Pool 4],,,,,
clar_2400:3114,,,[MIG_RLP_3114],40.000,[Pool 4],,,,,
clar_2400:261,[if00p02wi],if00p02wi(4),[if00p_L],60.000,[Pool 0],,,,,
clar_2400:265,[if00p02wi],if00p02wi(4),[if00p_L],65.000,[Pool 0],,,,,
clar_0986:208,[recindc],rec-cj.hq.nt.newyorklife.com(4),[erec-cj_R1],950.000,[Pool 1],BAU_erec,erec-208-1408-0996-2828,[erec1405_R2],clar_233:1408

Reference File
Code:
clar_2400:3113=clar_2400:3113[2323]
clar_2400:3114=clar_2400:3114[3333]
clar_2400:261=clar_2400:261[2323]
clar_2400:265=clar_2400:265[4444]
clar_0986:208=clar_0986:208[5656]
clar_233:1408=clar_233:1408[4545]

Output
Code:
 
clar_2400:3113[2323],,,[MIG_RLP_3113],40.000,[Pool 4],,,,,
clar_2400:3114[3333],,,[MIG_RLP_3114],40.000,[Pool 4],,,,,
clar_2400:261[2323],[if00p02wi],if00p02wi(4),[if00p_L],60.000,[Pool 0],,,,,
clar_2400:265[4444],[if00p02wi],if00p02wi(4),[if00p_L],65.000,[Pool 0],,,,,
clar_0986:208[5656],[recindc],rec-cj.hq.nt.newyorklife.com(4),[erec-cj_R1],950.000,[Pool 1],BAU_erec,erec-208-1408-0996-2828,[erec1405_R2],clar_233:1408[4545]

Thanks
# 2  
Old 09-21-2013
Has been solved a zillion times in here:
Code:
awk     'FNR==NR        {T[$1]=$2;next}
         ($1 in T)      {$1=T[$1]}
         1
        ' FS="=" file1 FS="," OFS="," file
clar_2400:3113[2323],,,[MIG_RLP_3113],40.000,[Pool 4],,,,,
clar_2400:3114[3333],,,[MIG_RLP_3114],40.000,[Pool 4],,,,,
clar_2400:261[2323],[if00p02wi],if00p02wi(4),[if00p_L],60.000,[Pool 0],,,,,
clar_2400:265[4444],[if00p02wi],if00p02wi(4),[if00p_L],65.000,[Pool 0],,,,,
clar_0986:208[5656],[recindc],rec-cj.hq.nt.newyorklife.com(4),[erec-cj_R1],950.000,[Pool1],BAU_erec,erec-208-1408-0996-2828,[erec1405_R2],clar_233:1408

# 3  
Old 09-21-2013
Thanks for the quick response ..... but its not always the first record ...
for example the last record on the last line in the input file needs to be matched too ...

clar_233:1408=clar_233:1408[4545]
# 4  
Old 09-21-2013
Sorry, should have read your spec a bit more carefully.
Code:
awk     'FNR==NR        {T[$1]=$2;next}
                        {for (i=1;i<=NF;i++) if ($i in T) $i=T[$i]}
         1
        ' FS="=" file1 FS="," OFS="," file
 
clar_2400:3113[2323],,,[MIG_RLP_3113],40.000,[Pool 4],,,,,
clar_2400:3114[3333],,,[MIG_RLP_3114],40.000,[Pool 4],,,,,
clar_2400:261[2323],[if00p02wi],if00p02wi(4),[if00p_L],60.000,[Pool 0],,,,,
clar_2400:265[4444],[if00p02wi],if00p02wi(4),[if00p_L],65.000,[Pool 0],,,,,
clar_0986:208[5656],[recindc],rec-cj.hq.nt.newyorklife.com(4),[erec-cj_R1],950.000,[Pool 1],BAU_erec,erec-208-1408-0996-2828,[erec1405_R2],clar_233:1408[4545]

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk to replace values in one file using a second reference file

Hi, I'd be grateful for your help with the following: I have a file with a single column (file1). Let's say the values are: a b c 5 d I have a second, reference file (ref_file), which is colon-delimited, and is effectively a key. Let's say the values in it are: a:1 b:2 c:3 d:4... (4 Replies)
Discussion started by: aberg
4 Replies

2. Shell Programming and Scripting

Keeping record of file 2 based on a reference file 1 in awk

I have 2 input files (tab separated): file1: make_A 1990 foo bar make_B 2010 this that make_C 2004 these those file2: make_X 1970 1995 ref_1:43 ref_2:65 make_A 1970 1995 ref_1:4 ref_2:21 ref_3:18 make_A 1980 2002 ref_1:7 ref_2:7 ref_3:0 ... (2 Replies)
Discussion started by: beca123456
2 Replies

3. Shell Programming and Scripting

Pattern match and replace indirect directory reference using sed

Hi, I need a ksh script to replace indirect directory references in an .ini file with a env variable using sed or awk. The .ini file is for example as such: A=.. B=../ C=../.. D=../../ E=../bin F=../../bin G=../../bin/xml H=../../bin/xml/ Need to replace an instance of .. or... (2 Replies)
Discussion started by: andyatit
2 Replies

4. Shell Programming and Scripting

Replace a value using a reference data from other file

Gents, Can you please help me to solve this case In my input file I have a values in column 49 which always need to be one, but sometimes the system create a value 2, in this case I need to go to search in the original file and replace the values in the row where the value 2 is and in the... (6 Replies)
Discussion started by: jiam912
6 Replies

5. Shell Programming and Scripting

[Solved] Replace records according to reference

Dear All, I am struggling with the following task and would appreciate some help. I have a large table (file1.txt). The first column of this table contains an ID. I would like to replace the ID with a label according to a reference file. Here is an example: cat infile.txt 0 AJ2312 310 ... (7 Replies)
Discussion started by: GDC
7 Replies

6. Shell Programming and Scripting

How to reference 2 dimensional array in awk?

Hello, all For a 1-dimensional array, such as myarr_1=1 myarr_1=2 myarr_1=3I know I can write a loop as below to show the array member one by one: for (i in myarr_1){print i, myarr_1}Now, suppose I have a two dimensional array such as: myarray_2=1 myarray_2=2 myarray_2=10 myarray_2=20My... (3 Replies)
Discussion started by: littlewenwen
3 Replies

7. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (1 Reply)
Discussion started by: perl_beginner
1 Replies

8. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (2 Replies)
Discussion started by: perl_beginner
2 Replies

9. Shell Programming and Scripting

Replace character based on reference file problem asking

I got two files right now, input file (target file), reference file 1 (query file) reference file 1 (long list of data) KOLOPWMOPOPO ADASDASD ADSASDASDAD . . target file (one long liner content) ADASDASDTYUKOKOLOPWMOPOPOOPLUAADSASDASDADPOPOUYADADASDASD desired output file content ... (1 Reply)
Discussion started by: patrick87
1 Replies

10. UNIX for Dummies Questions & Answers

awk language reference: book

Gurus, I am looking for a book on Awk programming. A quick Google search gave me Amazon.com: The AWK Programming Language (9780201079814): Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger: Books by Alfred Aho. Is there anything other than that? Any advice? Thanks. Al. (1 Reply)
Discussion started by: alan
1 Replies
Login or Register to Ask a Question