Operating System - HP-UX
1754014 Members
4821 Online
108811 Solutions
New Discussion юеВ

Re: Easy Points - sed/awk help again

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

Easy Points - sed/awk help again

I know this will be easy for you guys/girls, but how can I take a text file and covert the first letter of each field to caps. I know I could somehow do it with tr and cut but figured you may know of a better way.

Sample before data:
robert d anderson
steven g johnson
joseph wilson
andrew j jones

Sample after data:
Robert D Anderson
Steven G Johnson
Joseph wilson
Andrew J Jones

Thanks again,
Greg
8 REPLIES 8
Kelli Ward
Trusted Contributor
Solution

Re: Easy Points - sed/awk help again

Hi Greg,

This is kid of beside the point, but I posted a question recently for online learning sites, in it are a couple of nice pages on awk and sed.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xf5384b3ef09fd611abdb0090277a778c,00.html

Also, if you have some good online learning sites and want to share, post them and I'll give you easy points. ;)

Thanks,
Kel
The more I learn, the more I realize how much more I have to learn. Isn't it GREAT!
Kelli Ward
Trusted Contributor

Re: Easy Points - sed/awk help again

"This is kid of beside the point."
(That's cute! Meant kind.)
N/A please.
Kel
The more I learn, the more I realize how much more I have to learn. Isn't it GREAT!
Sridhar Bhaskarla
Honored Contributor

Re: Easy Points - sed/awk help again

Hi Greg,

Would you mind if I don't use awk and sed?.

Following is one quick primitive way of doing.
"file" is where your data is.

cat file |while read LINE
do
for WORD in $LINE
do
FIRST=`echo $WORD |cut -c 1|tr -s '[:lower:]' '[:upper:]'`
NEXT=`echo $WORD |cut -c 2-`
printf "${FIRST}${NEXT} "
done
printf "\n"
done

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Tom Danzig
Honored Contributor

Re: Easy Points - sed/awk help again

awk '{for(i=1;i<=$NF;i++){x=toupper(substr($i,1,1));y=substr($i,2);printf("%c%s ",x,y)};printf("\n")}
Rodney Hills
Honored Contributor

Re: Easy Points - sed/awk help again

Greg,

Here is a short perl routine.

cat file | perl -e 'while(<>) { while(/(\S+)(\s+)/g) {print ucfirst($1),$2 }}'

Hope this helps.

-- Rod Hills
There be dragons...
Tom Maloy
Respected Contributor

Re: Easy Points - sed/awk help again

How about perl?

cat test | perl -e 'while (<>) {s/(\w+)/\u$1/g;print}'

Tom
Carpe diem!
OneNeck UNIXSA
Frequent Advisor

Re: Easy Points - sed/awk help again

How about this one

fasprd01:/home/tjohnson:570$ cat tom.txt
robert d anderson
steven g johnson
joseph wilson
andrew j jones
fasprd01:/home/tjohnson:571$ cat tom.txt | awk -f cap1stletter.awk
Robert D Anderson
Steven G Johnson
Joseph Wilson
Andrew J Jones
fasprd01:/home/tjohnson:572$ cat cap1stletter.awk
# capitalize 1st letter in string

function capital(input, result, words, n, i, w)
{
result = ""
n = split(input, words, " ")
for (i - 1; i <= n; i++) {
w = words[i]
w = toupper(substr(w, 1, 1)) substr(w, 2)
if (i > 1)
result = result " "
result = result w
}
return result
}

{ print capital($0) }
fasprd01:/home/tjohnson:573$
Greg Stark_1
Frequent Advisor

Re: Easy Points - sed/awk help again

Thanks everyone for the quick response. Sorry Kelli, I got carried away with the point dropdowns on your second reply.

Greg