Replace from taking from file

Hi,

I have one file. S_CHAR.cfg. Content given below.

\" abcd1234
\| abcd1235
\$ abcd1236

So it has two record per line delimited by space.

No I am looking for some help on how to replace this special charaters ( mentioned in record 1 of each line) with the values mentioned in S_CHAR.cfg file. ( 2nd record).

So for example if I have one file TEST.out with the content like

Great \"India\"
Great \"World\"
I am loving every \$ I earn.

After replacing it will look like

Great abcd1234Indiaabcd1234
Great abcd1234Worldabcd1234
I am loving every abcd1236 I earn.

Thanks in advance.

Replace them where? In the same file? What is your expected output?

If you want to replace the entire 1st field with 2nd,

awk '$1 = $2' file

Sorry for incomplete requirement. Please check now. I have updated my post.

awk 'NR == FNR{a[$1] = $2; next}
  {for(x in a)
    {gsub(x, a[x])};
    print}' S_CHAR.cfg TEST.out
2 Likes

Hi ,
Will it be performance wise good, if I take one loop and replace 1st records with 2nd globally using SED?

---------- Post updated at 12:55 PM ---------- Previous update was at 12:49 PM ----------

One problem with this. It's keeping the "\" in final output.

Output I got:

Great \a1b45cd56India\a1b45cd56
Great \a1b45cd56World\a1b45cd56
I am loving every \a1b45cd57 I earn.

Also it's replacing normal quotes(") and dollar($).

awk 'NR == FNR{a[$1] = $2; next}
  {for(x in a)
    {gsub(x, a[x])}; gsub(/\\/, X);
    print}' S_CHAR.cfg TEST.out
2 Likes

It's replacing even if there is no backslash.

For example when input is

"G"rea"t \"India\""
"Gr"ea"t \"World\""
"I "am loving every \$ I earn."

I am getting output as

a1b45cd56Ga1b45cd56reaa1b45cd56t a1b45cd56Indiaa1b45cd56a1b45cd56
a1b45cd56Gra1b45cd56eaa1b45cd56t a1b45cd56Worlda1b45cd56a1b45cd56
a1b45cd56I a1b45cd56am loving every a1b45cd57 I earn.a1b45cd56

Can you please handle this one as well?