Computing.Net > Forums > Unix > replace Nth column

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

replace Nth column

Reply to Message Icon

Name: john1990
Date: February 1, 2006 at 00:36:27 Pacific
OS: unix
CPU/Ram: 640
Comment:

Hi Firend,

Need a help in awk/sed/tr/ perl .my file is very huge, so need some fast method.

My request.
=========
1. File is delimited by |
2. in 1st column i want to replace 7 by 1 and 9 by 2
3. in 5th column need to mask first 3 charcter by XXX


input file File
=========
790|6003949 |6003949 |168|Moorthy
790|6004376 |6004376 |168|Jet Lee
790|6004418 |6004418 |168|George Philip
790|6004582 |6004582 |168|Bush America
790|6004699 |6004699 |168|John Paul

output shoud be
===========
120|6003949 |6003949 |168|XXXrthy
120|6004376 |6004376 |168|XXX Lee
120|6004418 |6004418 |168|XXXrge Philip
120|6004582 |6004582 |168|XXXh America
120|6004699 |6004699 |168|XXX Paul



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 1, 2006 at 09:06:28 Pacific
Reply:

Hi:

Since I'm using Solaris, note my use nawk vs. awk:

#!/bin/ksh

nawk ' BEGIN { FS="|"; OFS="|" }
{
# replace 7 by 1 in column 1
sub("7", "1", $1)
# replace 9 by 2 in column 1
sub("9", "2", $1)
# replace first 3 chars in column 5 by XXX
sub("^...", "XXX", $5)

print $0
} ' yl.txt



0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: replace Nth column

replace a column with a new column www.computing.net/answers/unix/replace-a-column-with-a-new-column/7124.html

column substitution www.computing.net/answers/unix/column-substitution/7157.html

passing files as argument www.computing.net/answers/unix/passing-files-as-argument/7161.html