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
# 8  
Old 05-01-2010
Tools

Hope you will also give my solution a chance, as I have put too much effort in it.

capitalize
Code:
# capitalize each word in a string
function capitalize(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
}

# main program, for testing
{ print capitalize($0) }

Code:
$ awk -F"," '{$2=tolower($2); print $2}' file | nawk -f capitalize > tfile1
$ awk -F"," '{print $2}' file > tfile2
$ touch ofile
$ exec 3< tfile2
$ while read line1
do
read line2 <&3
sed ''/"$line2"/s//"$line1"/'' file | grep "$line1" >> ofile
done < tfile1
$ rm tfile1 tfile2

A question for the awk pro's:
While I was trying to avoid creating of temporary files, I managed to put the output of the first two commands in two variables
Code:
formatted=$(the first awk command here)
tosed=$(the second awk command here)

, but I didn't manage to split the content, so I'd really like to know how to put it in two shell arrays instead.
# 9  
Old 05-01-2010
Hi to all,

Frankin,

Thanks for your answer, the script it works perfect. I´ll try to understand better the use you gave it to functions you´ve used. Great!.

Scrutinizer,

I´ve tried your solution, but It looks does not change to upper case only first letter on column2, instead adds the
text within column 2 and last line isn´t printed. What could it be? Below the output I receive:

Code:
PRODUCT No.,SCIENCECIENCE BOOKSOOKS,DESCRIPTION
Product 1,PHILOSOPHIAEHILOSOPHIAE NATURALISATURALIS PRINCIPIARINCIPIA MATHEMATICAATHEMATICA (1687)1687),blah blah blah
Product 2,Dialogueialogue concerningoncerning thehe twowo chiefhief worldorld systemsystems (1632)1632),Blah blah blah
Product 3,Dee Revolutionibusevolutionibus Orbiumrbium Coelestiumoelestium (1543)1543),Blah blah blah

pseudocoder,

I appreciate really your effort in try to help me. Thanks really and certainly from each contribution we learned a lot.

But I don´t know why it doesn´t work for me. I copied the "capitalized" function code in a file called "capitalized"
and the input file saved in the same directory named only "file" like in your script. Then I ran the awk script until last
line (rm tfile1 ...) and creates this temp files with permission denied access. They cannot remove and the ofile is created
with 0 KB. I´m not sure why this happens.

Thanks again to all for just great help.

Best regards,
# 10  
Old 05-01-2010
Quote:
Originally Posted by cgkmal
... 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?
...
The character following the "-F" switch is your file's delimiter. Since your file's delimiter was a comma (",") in your first post, I put a comma over there.

The first argument of the join() function is the delimiter you want in your output. Since your first post specified the delimiter of the output as a comma (","), I put a comma over there as well.

Based on the information above, you should be able to fix the one-liner to suit your needs.

tyler_durden
# 11  
Old 05-02-2010
That´s right durden, I had done that, I mean, replace the (-F,) to (-F"|"), in this way.
Code:
perl -F"|" -lane '$F[1]=~s/(\w+)/ucfirst(lc($1))/ge; print join("|",@F)' inputfile

And with this inputfile:
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 |

I get every letter piped.
Code:
$ perl -F"|" -lane '$F[1]=~s/(\w+)/ucfirst(lc($1))/ge; print join("|",@F)' inputfile
P|R|O|D|U|C|T| |N|o|.|||S|C|I|E|N|C|E| |B|O|O|K|S|||D|E|S|C|R|I|P|T|I|O|N||
P|R|o|d|u|c|t| |1|||P|H|I|L|O|S|O|P|H|I|A|E| |N|A|T|U|R|A|L|I|S| |P|R|I|N|C|I|P|I|A| |M|A|T|H|E|M|A|T|I|C|A| |(|1|6|8|7|)|||b|l|a|h| |b|l|a|h| |b|l|a|
h||
P|R|o|d|u|c|t| |2|||D|i|a|l|o|g|u|e| |c|o|n|c|e|r|n|i|n|g| |t|h|e| |t|w|o| |c|h|i|e|f| |w|o|r|l|d| |s|y|s|t|e|m|s| |(|1|6|3|2|)|||B|l|a|h| |b|l|a|h| |
b|l|a|h||
P|R|o|d|u|c|t| |3|||D|e| |R|e|v|o|l|u|t|i|o|n|i|b|u|s| |O|r|b|i|u|m| |C|o|e|l|e|s|t|i|u|m| |(|1|5|4|3|)|||B|l|a|h| |b|l|a|h| |b|l|a|h||
P|R|o|d|u|c|t| |4|||t|h|e| |v|o|y|a|g|e| |o|f| |t|h|e| |b|e|a|g|l|e| |(|1|8|4|5|)|||B|l|a|h| |b|l|a|h| |b|l|a|h| ||

Again thanks for your help.

Regards.
# 12  
Old 05-02-2010
Quote:
Originally Posted by cgkmal
...
and creates this temp files with permission denied access. They cannot remove and the ofile is created
with 0 KB. I´m not sure why this happens.
Very strange, unfortunately I can't tell why that happens.
It pretty certainly does not have anything to do with the code.
In practice a "echo test > tfile" would also produce a no-access file.
Logically the ofile is empty, because the while loop had no access to tfile1 and tfile2.
It would be very nice to know what happens if you redirect "> tfile" the output of Franklin52 code into tfile.
In practice you also should have that permission denied access issue.
Please let me know.
# 13  
Old 05-02-2010
Hi pseudocoder,

I´ve run your code afresh, this time generates again the 3 files (ofile, tfile1 and tfile2) with 0KB, but without denied permission. I redirected the franklin code to each of 3 files above and were fill with the correct output.

Strange like you said, I don´t know what could happened.

Thanks for your help again
# 14  
Old 05-02-2010
I almost can't believe it...
I know Franklin's code is definitely the best solution for you, but for better understanding I'd like to know what happens if you just run
Code:
awk -F"," '{$2=tolower($2); print $2}' file | nawk -f capitalize

and
Code:
awk -F"," '{print $2}' file

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