Replace a column with a value by ignoring the header lines

i have a file in the gz format , the content of the file is as follow.

gzcat f1.gz

# 1.name
# 2.location
# 3.age
# 4.dob
.
.
.
.
.
.
.
.
.
# 43.hobbies

< Aravind,33,chennai,09091980, , , , , , , surfing>
< Swapnil,32,bhopal,01121981, , , , , , , cricket>

i want to hide the DoB 4th column with ******* and any suggestion please.

gzcat f1.gz | nawk '{gsub("09091980","********",$5)}1' | more

by doing this i am able to do line by line only , how to do it at one shot , any suggestion please.

Is it possible to do without disturbing the first 45 lines where it indicates the description of the positions.

gzcat ANSIAC*.gz | nawk 'NR>45{sub(".{8}","********",$5)}1'
1 Like

I guess anbu23 has missed out to specify the FS :slight_smile:

Hello Thanks Anbu,

the f1.gz output is slightly modified , the script given by is working if there is no space after comma.

gzcat f1.gz | more

< Aravind, 33, chennai, 09091980, , , , , , , surfing>
< Swapnil, 32, bhopal, 01121981, , , , , , , cricket>

after comma there is space . how to modify the code for the same.

one more thing is it possible to recreate output with the same file name with changes in the code :slight_smile:

if yes please tell the same it would be of great help.

I guess, setting FS to , should help

gzcat ANSIAC*.gz | nawk -F"," 'NR>45{sub($3," ********",$3)}1' OFS=","
$ cat file
< Aravind, 33, chennai, 09091980, , , , , , , surfing>
< Swapnil, 32, bhopal, 01121981, , , , , , , cricket>
$ awk '{sub(".{8}","********",$5)}1' file
< Aravind, 33, chennai, ********, , , , , , , surfing>
< Swapnil, 32, bhopal, ********, , , , , , , cricket>
$ cat file
< Aravind,33,chennai,09091980, , , , , , , surfing>
< Swapnil,32,bhopal,01121981, , , , , , , cricket>
$ awk -F"," -v OFS="," '{sub(".{8}","********",$4)}1' file
< Aravind,33,chennai,********, , , , , , , surfing>
< Swapnil,32,bhopal,********, , , , , , , cricket>

Redirect the output of awk to same file and then gzip it.