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
# 15  
Old 05-02-2010
I run these codes and worked, but the only change I made is use gawk or awk instead of nawk (I havenīt installed it).
Code:
$ awk -F"|" '{print $3}' file
SCIENCE BOOKS
PHILOSOPHIAE NATURALIS PRINCIPIA MATHEMATICA (1687)
Dialogue concerning the two chief world systems (1632)
De Revolutionibus Orbium Coelestium (1543)
the voyage of the beagle (1845)

Code:
$ awk -F"|" '{$3=tolower($3); print $3}' file | gawk -f capitalize
Science Books
Philosophiae Naturalis Principia Mathematica (1687)
Dialogue Concerning The Two Chief World Systems (1632)
De Revolutionibus Orbium Coelestium (1543)
The Voyage Of The Beagle (1845)

I think this probes your code works fine, I donīt now why run it in the other way failed to me.

Anyway, thanks for your interest to help.

Best regards,
# 16  
Old 05-02-2010
All right, I'm glad we could work it out.
Thank you for testing and giving me feedback.
# 17  
Old 05-03-2010
Quote:
Originally Posted by cgkmal
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

That's wrong, cgkmal. The "-F" switch is followed by the pattern to split on. It's a regular expression pattern and not a literal string. You can enclose it within double-quotes or single-quotes, but that doesn't make it a literal string. The character "|" has a special meaning in regular expression terminology; it is the "OR" operator.

If you want to specify the literal pipe character as the delimiter, then you'll have to remove the special meaning of "OR" operator. And you do that by escaping the OR operator like so "\|".

So your command line should be:

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

Quote:
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| ||

...
The OR operator "|", when supplied as a delimiter to the split function, works like the undef or null string. The net effect is that it splits on each character. Note how the operation on $F[1] worked only on the second character - that's because after the split, $F[1] was the character "R" or "r" of your inputfile.

The one-liner below should make that clear:

Code:
$
$
$ echo "abcd|1234" | perl -F"|" -lane 'print $F[1]'
b
$
$ # In this one-liner, the input was split on each character due to "|"
$ # So, $F[1] was the second element of array @F i.e. "b"
$
$
$ echo "abcd|1234" | perl -F"\|" -lane 'print $F[1]'
1234
$
$ # In this one-liner, the input was split on correctly due to "\|"
$ # So, $F[1] was the second element of array @F i.e. "1234"
$

HTH,
tyler_durden
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