If you save the script vgersh99 suggested in a file named kam.awk , you need to use the same name in the command line when you invoke awk :
awk -f kam.awk Milano Torino Firenze
I assume the <comma> instead of <period> in the filename in the command line was a typo.
With vgersh99's script the order of cities in the output is in the same order as the input files, but the order of users in the output is random and may vary depending on which version of awk runs your script. If you want to guarantee that the order of users in the output matches the order in which user names were first seen in input files, you could try this slightly more complicated script. If you save the following in a file named merger :
#!/bin/ksh
awk '
BEGIN { # Set output field separator.
OFS = ";"
}
FNR == 1 {
# Gather city names from filenames.
city[++ncity] = FILENAME
}
{ # Gather data from the current input file.
# Have we seen this user beofore?
if(!($1 in user)) {
# No. Add this user to the list of know users...
user[$1]
# and keep track of the order in which users were found.
order[++nuser] = $1
}
# Note that we have seen this user in this city.
assigned[$1, ncity]
}
END { # Print header...
printf("User%s", OFS)
for(i = 1; i <= ncity; i++)
printf("%s%s", city, (i == ncity) ? ORS : OFS)
# Print data for each user...
for(i = 1; i <= nuser; i++) {
# print user name...
printf("%s%s", order, OFS)
for(j = 1; j <= ncity; j++)
# and print assigned data for each city.
printf("%s%s",
((order, j) in assigned) ? "assigned" : "",
(j == ncity) ? ORS : OFS)
}
}' "$@"
This was written and tested with a Korn shell, but will work with any shell that uses Bourne shell syntax (including bash and several others). If you want to run this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .