Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filling in blank field at the start of csv file 1

Status
Not open for further replies.

ianicr

IS-IT--Management
Joined
Nov 4, 2003
Messages
230
Location
GB
I've got a name and address csv file which needs a title field at the start. however some already have the field filled in. Is there a way to add a Mr to the first field if there is nothing in there eg
,J,Smith
becomes:
Mr,J,Smith
but
Dr,J,Brown
stays the same?
 
This assumes that that missing title is always "Mr". You may need to add to the list of titles in the split statement.

BEGIN {
n=split("Mr Dr Ms Miss Mrs",a)
FS=","
}
{
for (j=1;j<=n;j++) {
if ($1 == a[j]) {
print
next
}
}
print &quot;Mr&quot; $0
}

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
You can try this:
Code:
sed 's!^ *,!Mr,!' <input.csv >output.csv

Hope This Help
PH.
 
Thanks. I used the sed and it worked fine. Another quick one. I've got a csv file with addresses in and I need to add a comma after the house number eg.
1 new street, becomes
1, new steet,
The problem I'm having is the variable size of huose number eg. 1 new street, 12 new street or 123 new street. Any ideas?
 
sed -e 's/^\([^ ][^ ]*\)\(.*\)/\1,\2/g' file

NOTE: doing 'man sed' might be helpful.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top