Replace a column with a value by ignoring the header lines | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Replace a column with a value by ignoring the header lines

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-25-2013
aravindj80 aravindj80 is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 20 October 2014, 8:53 AM EDT
Posts: 44
Thanks: 3
Thanked 0 Times in 0 Posts
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.


Code:
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.

Last edited by aravindj80; 03-25-2013 at 06:47 AM..
Sponsored Links
    #2  
Old 03-25-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 8 October 2014, 1:35 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

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

The Following User Says Thank You to anbu23 For This Useful Post:
aravindj80 (03-25-2013)
Sponsored Links
    #3  
Old 03-25-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 10 July 2014, 12:57 PM EDT
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
I guess anbu23 has missed out to specify the FS
    #4  
Old 03-25-2013
aravindj80 aravindj80 is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 20 October 2014, 8:53 AM EDT
Posts: 44
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks Anbu

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 :-)

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

Last edited by aravindj80; 03-25-2013 at 07:06 AM..
Sponsored Links
    #5  
Old 03-25-2013
PikK45 PikK45 is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 10 July 2014, 12:57 PM EDT
Location: Chennai
Posts: 567
Thanks: 43
Thanked 74 Times in 72 Posts
I guess, setting FS to , should help


Code:
gzcat ANSIAC*.gz | nawk -F"," 'NR>45{sub($3," ********",$3)}1' OFS=","

Sponsored Links
    #6  
Old 03-25-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 8 October 2014, 1:35 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts

Code:
$ 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>


Code:
$ 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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge all csv files in one folder considering only 1 header row and ignoring header of all others Shiny_Roy UNIX for Dummies Questions & Answers 4 10-21-2011 04:08 PM
Rename a header column by adding another column entry to the header column name Vavad UNIX for Dummies Questions & Answers 1 08-06-2011 01:02 PM
Rename a header column by adding another column entry to the header column name URGENT!! Vavad Shell Programming and Scripting 4 08-05-2011 12:35 PM
deleteing duplicate lines sing uniq while ignoring a column japaneseguitars UNIX for Dummies Questions & Answers 5 06-16-2010 09:50 AM
awk/sed column replace using column header - help jkl_jkl Shell Programming and Scripting 10 06-18-2008 03:31 AM



All times are GMT -4. The time now is 03:41 AM.