Convert to upper case first letter of each word in column 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert to upper case first letter of each word in column 2
# 1  
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.
# 2  
Old 05-01-2010
I don't know about sed or awk, but Perl empowers you to make this change rather trivially -

Code:
$ 
$ 
$ cat -n f7
     1    PRODUCT No.,SCIENCE BOOKS,DESCRIPTION
     2    Product 1,PHILOSOPHIAE NATURALIS PRINCIPIA MATHEMATICA (1687),Blah blah blah
     3    Product 2,Dialogue concerning the two chief world systems (1632),Blah blah blah
     4    Product 3,De Revolutionibus Orbium Coelestium (1543),Blah blah blah
     5    Product 4,the voyage of the beagle (1845),Blah blah blah
$ 
$ perl -F, -lane '$F[1]=~s/(\w+)/ucfirst(lc($1))/ge; print join(",",@F)' f7
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
$ 
$

tyler_durden
# 3  
Old 05-01-2010
Hi durden_tyler,

Thanks for your reply.

Perl is a excellent way, but I´m don´t know how to use it. I´ve tried your script and works fine with the sample file. In order to use in a real file that could be of 40 columns, how can I change the script to work for instance, only over column 25 or 35?

Thanks again.
# 4  
Old 05-01-2010
Quote:
Originally Posted by cgkmal
... Perl is a excellent way, but I´m don´t know how to use it. ...
You'll have to learn it if you want to use it. I couldn't think of any other alternative.

Quote:
Originally Posted by cgkmal
...In order to use in a real file that could be of 40 columns, how can I change the script to work for instance, only over column 25 or 35? ...
Arrays in Perl have zero-based indexes. $F[1] is the 2nd element, which corresponds to the 2nd column of your file. On the same lines, $F[24] refers to the 25th element and $F[34] to the 35th.

tyler_durden
# 5  
Old 05-01-2010
Your right durden, It could be nice to learn it for me.

By the moment, I changed a little bit the script considering a file of 40 columns an separated by pipe "|", but when I see the output many columns have their content disordered, looks like have been moved to other column.

The script I used is:

Code:
perl -F, -lane '$F[20]=~s/(\w+)/ucfirst(lc($1))/ge; print join("|",@F)' inputfile > output



What could it be?

Thanks in advance.
# 6  
Old 05-01-2010
You can play around with this example, adjust col to convert another column:
Code:
awk -F, -v col=2 ' 
NR > 1{
  n=split(tolower($col),a," ")
  $col=toupper(substr(a[1],1,1)) substr(a[1],2)
  for(i=2;i<=n;i++) {
    $col=$col " " toupper(substr(a[i],1,1)) substr(a[i],2)
  }
}1' OFS="|" infile

# 7  
Old 05-01-2010
Or ksh:
Code:
#!/bin/ksh
typeset -L1 -u first
typeset -l rest
while IFS=, read field1 field2 fieldn; do
  titlefield2=""
  for i in $field2; do
    first=$i
    rest=${i#?}
    titlefield2="$titlefield2$first$rest "
  done
  printf "%s,%s,%s\n" "$field1" "${titlefield2% }" "$fieldn"
done < infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
Login or Register to Ask a Question