echo 'His phone number is (123) 456-7890 and account is 34567 887272. ' | awk '
{while (match ($0, /[0-9()-]+ *[0-9()-]*/)) {X = substr ($0, RSTART, RLENGTH-4)
gsub (/[0-9]/, "*", X)
printf "%s%s%s", substr ($0, 1, RSTART-1), X, substr ($0, RSTART + RLENGTH - 4, 4)
$0 = substr ($0, RSTART + RLENGTH)
}
print
}
'
His phone number is (***) ***-7890 and account is ***** **7272.
The regex needs to be quite cumbersome as my mawk doesn't allow for back references. It will fail, too, if there are non-digits in the last four places.