Q:

Replacing numbers in the same range

I have a tab delim file1 where the first column looks like
1
1
6
6
6
1A
1A
1B
2
2
2
2
3
4
4A
Z
Z

I wanna replace so that 1 = 1, 1A = 2, 1B = 3, 2 = 4, 3 = 5, 4 = 6, 4A = 7, 6 = 8, Z = 9
Output should then be
1
1
8
8
8
2
2
3
4
4
4
4
5
6
7
9
9

I've sen other posts saying that to avoid cumulative substitution I should work my way from bigger values to smaller ones and do the multiple substitutions using:
sed -e 's/a/b/g ; s/b/d/g' file

but I have 60 substitutions to make. 
Is there a way I can do this in a loop or some other alternative instead of writing 60 substitutions 
Notice each element is repeated different times and they can be numbers and characters, but I'm substituting them all for numbers 1-60, and in a predefined order and not in the order they show up. My file has other columns with the same characters, but I only wanna substitute things in the 1st column.

A:

Create a mapping file with your search and replacement strings as:
cat mapping
1   1
1A  2
1B  3
2   4
3   5
4   6
4A  7
6   8
Z   9

Then simply use this awk to get all substitutions in single command:
awk 'BEGIN{FS=OFS="\t"} NR == FNR{key[$1]=$2; next} $1 in key{$1=key[$1]} 1' mapping file

1
1
8
8
8
2
2
3
4
4
4
4
5
6
7
9
9

