Sorry for my late response. I've tried with gensub as below, but is replacing all "" with "-" in 3rd column
and I don't want to replace the first "" and last "_", only replace from the 2nd underscore to n-1 underscore.
I'm not familiar with gensub(). Assuming that there are at least 2 underscores in field 3, you could try something like:
awk '
{ n = split($3, x, /_/)
printf("%s %s %s_", $1, $2, x[1])
for(i = 2; i <= n; i++)
printf("%s%s", x, i == n ? "\n" : i == (n - 1) ? "_" : "-")
}' input
This should work even if there are underscores in field 1 or field 2.
If there will never be any underscores in field 1 or field 2 and there are at least two underscores in field 3, anbu23's suggestion should also work if you change the last printf from printf("%s\n",FS NF) to printf("%s\n",FS $NF) .
If you want to run either of these script on a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of the default /usr/bin/awk .