Not an answer, just to show a shorter, idiomatic way to populate the table[] array from @konsolebox's answer, as discussed in the relevant comments:
BEGIN { split("aeb", old) split("x ch o", new) for (i in old) table[old[i]] = new[i] FS = OFS = "" }
so the mapping of old to new characters is clearly shown in that the char in the first split () maps to char (s) below it and for any other mapping you want, you just need to change the line in split (), doesn't change 26- explicit assignments to table [].
You can even create a generic script to do mappings and simply pass old and new lines as variables:
BEGIN { split(o, old) split(n, new) for (i in old) table[old[i]] = new[i] FS = OFS = "" }
then in the shell nothing like this:
old="aeb" new="x ch o" awk -vo="$old" -vb="$new" -f script.awk file
and you can protect yourself from your mistakes by filling in the lines, for example:
BEGIN { numOld = split(o, old) numNew = split(n, new) if (numOld != numNew) { printf "ERROR: #old vals (%d) != #new vals (%d)\n", numOld, numNew | "cat>&1" exit 1 } for (i=1; i <= numOld; i++) { if (old[i] in table) { printf "ERROR: \"%s\" duplicated at position %d in old string\n", old[i], i | "cat>&2" exit 1 } if (newvals[new[i]]++) { printf "WARNING: \"%s\" duplicated at position %d in new string\n", new[i], i | "cat>&2" } table[old[i]] = new[i] } }
It would not be good to know if you wrote that b displays x, and then mistakenly wrote that b displays y? The above is really the best way to do this, but your challenge, of course.
Here is one complete solution, as described in the comments below
BEGIN { numOld = split("aeb", old) numNew = split("x ch o", new) if (numOld != numNew) { printf "ERROR: #old vals (%d) != #new vals (%d)\n", numOld, numNew | "cat>&1" exit 1 } for (i=1; i <= numOld; i++) { if (old[i] in table) { printf "ERROR: \"%s\" duplicated at position %d in old string\n", old[i], i | "cat>&2" exit 1 } if (newvals[new[i]]++) { printf "WARNING: \"%s\" duplicated at position %d in new string\n", new[i], i | "cat>&2" } map[old[i]] = new[i] } FS = OFS = "" } { for (i = 1; i <= NF; ++i) { if ($i in map) { $i = map[$i] } } print }
I renamed the table array as map only because iMHO better reflects the purpose of the array.
save above in script.awk file and run it as awk -f script.awk inputfile