Replace a field with a character as per the field length

Hi all,
I have a requirement to replace a field with a character as per the length of the field.
Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (). like *******************

As the field is not a fixed one, i want to do the replacement according to the field length(i.e. length($2) or $3 ....).

please help me to get the solution.

Thanks in adv.

awk '{ for(i=1;i<=NF;i++) if(length($i)==20) gsub(/./,"*",$i); }1' filename
1 Like

Example to replace fields 2, 3 and 19 with '*' to field length (subsitute with the field#s you want to replace).

awk 'BEGIN{split("2,3,19", F, ",")} {for(fld in F) gsub(/./, "*", $fld)}1' filename
1 Like