A short awk
program will achieve this for you
awk -F': ' '
# Every line of input; fields split at colon+space
{
# Append a comma if we have previous items
if (h[$1] > "") { h[$1] = h[$1] ", " };
# Append the item and increment the count
h[$1] = h[$1] $2;
i[$1]++
}
# Finally
END {
# Iterate across all the keys we have found
for (k in h) {
if (i[k] > 1) { p = "[%s]" } else { p = "%s" };
printf "%s: " p "\n", k, h[k]
}
}
' data.txt
Output
hmz_age: ['21', '41']
tom_age: '31'
fd_year_anne: ['1987', '1982']
school: ['anne', 'svp']
name: ['tom', 'hmz', 'toli']
status_hmz_mar: 'no'
status_tom_mar: 'yes'