string replacement using a lookup table

Dear all thanks for helping in advance.. Know this should be fairly simple but I failed in searching for an answer.

I have a file (replacement table) containing two columns, e.g.:

ACICJ ACIDIPHILIUM
ACIF2 ACIDITHIOBACILLUS
ACIF5 ACIDITHIOBACILLUS
ACIC5 ACIDOBACTERIUM
ACIC1 ACIDOTHERMUS
ACIAC ACIDOVORAX

which are tab or space delimited (which can be changed easily)

And need to use this table to replace in another file all occurances of strings from the first column by their counterpart strings. Can this be done easily in bash shell?

Thankx again

Post an example of the other file and the desired output with the given replacement table.

Example input and output would be like the attached. The table would be used to identify and replace the strings..

# sed 's/F5\|F2/DITHIOBACILLUS/g;s/CJ/DIPHILIUM/;s/C5/DOBACTERIUM/' infile
(PARDP_1_PE905,(ACIDITHIOBACILLUS_1_PE2435,ACIDITHIOBACILLUS_1_PE2737)100)80)88;
(ACIDIPHILIUM_2_PE3051,METRJ_9_PE1586)95;
(ACIDOBACTERIUM_1_PE2202,ACIBL_1_PE895)96;
(ACIDITHIOBACILLUS_1_PE2306,ACIDITHIOBACILLUS_1_PE2606)100,MAGSM_1_PE17)92,

I guess this is an ad hoc suggestion, which will not do ( as the replacement table is hundreds and the input file - thousands of lines.

i cant know your file how many lines and line lenghths..

you can check this :wink:
sed, a stream editor

if you cant success try this..

 
# perl -pe 's/F5/DITHIOBACILLUS/g;s/F2/DITHIOBACILLUS/;s/CJ/DIPHILIUM/;s/C5/DOBACTERIUM/' infile

sed is a powerful thing, i have this page in bookmarks :), thanks
But still my lines to replace are many more..

If the template file (replacement table) has 100 lines, above sed or perl commands will be long to fill the whole web pages.:slight_smile:

$ cat template.txt
ACICJ ACIDIPHILIUM
ACIF2 ACIDITHIOBACILLUS
ACIF5 ACIDITHIOBACILLUS
ACIC5 ACIDOBACTERIUM
ACIC1 ACIDOTHERMUS
ACIAC ACIDOVORAX

$ cat input.txt
(PARDP_1_PE905,(ACIF5_1_PE2435,ACIF2_1_PE2737)100)80)88;
(ACICJ_2_PE3051,METRJ_9_PE1586)95;
(ACIC5_1_PE2202,ACIBL_1_PE895)96;
(ACIF5_1_PE2306,ACIF2_1_PE2606)100,MAGSM_1_PE17)92,

$ awk 'NR==FNR{a[$1]=$2;next}{ for (i in a) gsub(i,a)}1' template.txt input.txt  

(PARDP_1_PE905,(ACIDITHIOBACILLUS_1_PE2435,ACIDITHIOBACILLUS_1_PE2737)100)80)88;
(ACIDIPHILIUM_2_PE3051,METRJ_9_PE1586)95;
(ACIDOBACTERIUM_1_PE2202,ACIBL_1_PE895)96;
(ACIDITHIOBACILLUS_1_PE2306,ACIDITHIOBACILLUS_1_PE2606)100,MAGSM_1_PE17)92,

Maybe it's also safer to include underscore:

awk 'NR==FNR{a[$1 "_"]=$2 "_";next}{ for (i in a) gsub(i,a)}1' template.txt input.txt

Although the 1st and 3rd parts are in different forms: AAAA0 to AA0+

rdcwayx & konsolebox, thank you very much indeed. konsolebox: may I ask what is the safety from considering the underscore? U think otherwise awk may over-replace in some cases?

Yes I just see that probability.