Sponsored Content
Top Forums Shell Programming and Scripting Convert to upper case first letter of each word in column 2 Post 302417824 by cgkmal on Saturday 1st of May 2010 12:00:17 AM
Old 05-01-2010
Convert to upper case first letter of each word in column 2

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)

Code:
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.
Code:
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.

Code:
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

after i convert upper case to lowercase

If user chosen to tolower then it should convert file name to lower or vice versa. when file names converted it should put into appropriate subdirectories. e.g when files converted it then seperate them out with file etension where it will seperate them out . such as file.pdf, phone.doc both... (1 Reply)
Discussion started by: Alex20
1 Replies

2. UNIX for Dummies Questions & Answers

How to add a space after an Upper case letter

Hi I have two questions. 1. how to convert "EverythingIsFine" to "Everything Is Fine" in a txt file. 2. how to convert everything to upper case letter and reverse, I hope there is a general purpose script for this. (1 Reply)
Discussion started by: onthetopo
1 Replies

3. Shell Programming and Scripting

how to convert value in a variable from upper case to lower case

Hi, I have a variable $Ctrcd which contains country names in upper case and i want to convert them into lower case. I have tried so many solutions from already existing threads but couldn't get the correct one. Can anybody help me with this..... Thanks a lot.. (2 Replies)
Discussion started by: manmeet
2 Replies

4. Shell Programming and Scripting

convert upper case to lower case in ascript

I have a package to install and the installation script which does it . The files/directories names in the script are all lower case but the actual package has everything in upper case - file names, directories . I don't want to rename directories and files in the package - it has a lot of them . ... (2 Replies)
Discussion started by: vz6zz8
2 Replies

5. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

6. Shell Programming and Scripting

Convert first character of each word to upper case

Hi, Is there any function(Bash) out there that can convert the first character of each word to upper case?... (3 Replies)
Discussion started by: harchew
3 Replies

7. UNIX for Dummies Questions & Answers

To convert Lower case to Upper Case

There is a script where we pass the parameter in lower case: say: . ./scriptName pArameter #!/bin/ksh echo "`date` Entering $0 Reloading the $1 table " mname1=$1 (code to login MYSQL Database) Truncate table $mname1; exit ! Since now there is a limitaion of MYSQL that it accept... (5 Replies)
Discussion started by: ambarginni
5 Replies

8. Shell Programming and Scripting

SED (or other) upper to lowercase, with first letter of first word in each sentence uppercase

The title pretty much defines the problem. I have text files that are all in caps. I would like to convert them to lowercase, but have the first letter of the first word in each sentence in uppercase. I already have SED on the server for fixing / tweaking text files, but I'm open to other... (5 Replies)
Discussion started by: dockline
5 Replies

9. Shell Programming and Scripting

Upper case letter match

Hi, im able to search for string in a file (ex: grep -w "$a" input.txt). but i have to search for the uppercase of a string in a file where upper case of the file content matches something like below. where upper("$a")== converted to upper case string in (input.txt) can someone please provide... (5 Replies)
Discussion started by: p_satyambabu
5 Replies

10. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies
All times are GMT -4. The time now is 03:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy