Hi guys,
I have a file separated by ",". I�m trying to change to upper case the first letter of each word in column 2 to establish a standard format on this column.
I hope somebody could help me to complete the SED or AWK script below.
The file looks like this:
(Some lines in column 2 are all uppercase, some are lower case and some are mixed)
PRODUCT No.,SCIENCE BOOKS,DESCRIPTION
Product 1,PHILOSOPHIAE NATURALIS PRINCIPIA MATHEMATICA (1687),Blah blah blah
Product 2,Dialogue concerning the two chief world systems (1632),Blah blah blah
Product 3,De Revolutionibus Orbium Coelestium (1543),Blah blah blah
Product 4,the voyage of the beagle (1845),Blah blah blah
If I use SED, I can change to lower case every letter and then to upper case only first letter of every word, but sed changes over all columns.
sed -e 's/.*/\L&/' -e 's/\<./\u&/g' file
where:
sed 's/.*/\L&/' file --> Changes to lower case
sed 's/\<./\u&/g' file --> Changes only first letter of each word to uppercase
Is there a way to say SED to work over a specific column?
Well, with AWK, I�ve been trying using gensub function, but I don�t know how to
replace the matched pattern with the same pattern but in upper case.
awk 'BEGIN{FS=OFS=","} NR>1{$2=tolower($2);$2=gensub(/\<[A-Za-z]/,"X","g",$2)} {print $0}'
Where:
NR>1{$2=tolower($2) --> Changes all within column 2 to lower case
$2=gensub(/\<[A-Za-z]/,"X","g",$2) --> Replaces with "X" the first letter of each word (regexp \<[A-Za-z]) within column 2.
Is there a way to use toupper with a remembered pattern?
(i.e instead put "X", use toupper("\1") within gensub )
This awk script replaces the first letter with a constant (a capital X)
PRODUCT No.,SCIENCE BOOKS,DESCRIPTION
Product 1,Xhilosophiae Xaturalis Xrincipia Xathematica (1687),Blah blah blah
Product 2,Xialogue Xoncerning Xhe Xwo Xhief Xorld Xystems (1632),Blah blah blah
Product 3,Xe Xevolutionibus Xrbium Xoelestium (1543),Blah blah blah
Product 4,Xhe Xoyage Xf Xhe Xeagle (1845),Blah blah blah
Thanks in advance.