Bash Replace value in specific column

Hi all,

I have two files with the following format:

file1

BBB;33
AAA;2
CCC;5

file2

5;.;.;.
33;.;.;.

The first file contain a list of code and numbers. The second file only the number.
I would like to replace the corresponding code in the first column of the file1 with the corresponding value in column1 (and just in column1) of file2 so to have:

output

CCC;.;.;.
BBB;.;.;.

Hope this is clear,
Please notice that not all of the code are contained as number in file two and also the two file are not in order.

Thanks for suggestions.

awk 'NR==FNR { A[$2]=$1; next }; $1 in A { $1=A[$1] } 1' FS=";" OFS=";" file1 file2 > file3
1 Like

Thanks. Works really well. Could you please explain a little bit how it works?
Thanks.

awk '
# NR is the total number of lines processed so far
# FNR is the line number in the present file
# When they are the same, that means awk is reading the first file.
# So, when reading the first file, load elements into A like
# A["33"]="BBB" for easy lookup later.
# $1 and $2 are the first and second columns in the line, respectively.
NR==FNR { A[$2]=$1; next }

# If the first column is found in A's array indexes, then
# set the first column to the data for A[$1].
$1 in A { $1=A[$1] }

# Print every line ( Prints whenever the statement is true, and 1 is always true)
#
# FS is the input column separator, OFS is the output column separator.
# Any number of variables can be specified before files by putting VARNAME=value.
#
# After that comes the list filenames to process in order.
1'  FS=";" OFS=";" file1 file2 > file3