Remove usernames in a file and replace with a incremental number
As you have realized that you "don't need the 2 files", use the following awk
solution to process the initial log file in one pass:
awk '{
u_name = substr($5, 1, index($5, "@"));
if (!(u_name in users)) users[u_name] = ++c;
sub(/^[^@]+/, "USER" users[u_name], $5)
}1' file.log
The output:
12:00:00 (AAA) OUT: "string" USER1@hostname
12:00:00 (AAA) OUT: "string" USER1@hostname
12:00:00 (AAA) OUT: "string" USER2@hostname
12:00:00 (AAA) OUT: "string" USER1@hostname
12:00:00 (AAA) OUT: "string" USER3@hostname
Another awk
awk '!($5 in a){a[$5]=++i}{sub("[^@]*","USER"a[$5],$5)}1' infile
With bash you could do:
n=0
declare -A users=()
while IFS= read -r line; do
if [[ $line =~ ([^[:blank:]]+)@ ]]; then
user=${BASH_REMATCH[1]}
if [[ -z ${users[$user]} ]]; then
users[$user]=USER$((++n))
fi
line=${line/$user/${users[$user]}}
fi
echo "$line"
done < File2
or a perl one-liner
perl -pe 's/(\S+)(?=@)/ $users{$1} ||= "USER".++$n /e' File2