Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

regex - Using Sed to capitalize the first letter of each word

Here is the data I want to capitalize:

molly w. bolt     334-78-5443
walter q. bugg    984-49-0032
noah p. way       887-12-0921
kerry t. bricks   431-09-1239
ping h. yu        109-32-9845

Here is the script I have written so far to capitalize the first letter of name including initial

h
s/(.).*/1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/(.)
(.)(.*)/13/
/ [a-z]/{
h
s/([A-Z][a-z]* )([a-z]).*/2/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/(.)
([A-Z][a-z]* )(.)(.*)/214/
}
/ [a-z]/{
h
s/([A-Z][a-z]* )([a-z]).*/2/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/(.)
([A-Z][a-z]* )(.)(.*)/214/
}

It gives me:

MOLLY W. BOLT     334-78-544Molly 3. bolt     334-78-5443
WALTER Q. BUGG    984-49-003Walter 2. bugg    984-49-0032
NOAH P. WAY       887-12-092Noah 1. way       887-12-0921
KERRY T. BRICKS   431-09-123Kerry 9. bricks   431-09-1239
PING H. YU        109-32-984Ping 5. yu        109-32-9845

I want to only have:

Molly W. Bolt     334-78-544
Walter Q. Bugg    984-49-003
Noah P. Way       887-12-092
Kerry T. Bricks   431-09-123
Ping H. Yu        109-32-984

What would I change?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about this (GNU sed):

$ sed 's/[a-z]/u&/g' myfile
Molly W. Bolt     334-78-5443
Walter Q. Bugg    984-49-0032
Noah P. Way       887-12-0921
Kerry T. Bricks   431-09-1239
Ping H. Yu        109-32-9845

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...