Shell script to convert words to Title case


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Shell script to convert words to Title case
# 1  
Old 06-08-2014
Shell script to convert words to Title case

Hi Smilie

I have a .txt file with thousands of words.

I was wondering if i could use a simple sed or awk command to convert / replace all words in the text file to Title Case format ?

Example:

from:
Code:
this is line one
this is line two
this is line three

to desired output:
Code:
This Is Line One
This Is Line Two
This Is Line Three

Any help would be much appreciated.
Thank you!

Last edited by Scrutinizer; 06-08-2014 at 06:46 AM.. Reason: code tags
# 2  
Old 06-08-2014
Try:
Code:
$ awk '{for(j=1;j<=NF;j++){ $j=toupper(substr($j,1,1)) substr($j,2) }}1' file

Code:
sed -e "s/\b\(.\)/\u\1/g" file

---------- Post updated at 03:19 PM ---------- Previous update was at 03:16 PM ----------

Kindly use codetags!
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 06-08-2014
Note: the use of \b and \u is GNU sed only..
It can be further reduced to:
Code:
sed 's/\b./\u&/g' file


Last edited by Scrutinizer; 06-08-2014 at 06:59 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 06-08-2014
Thank you very much. I tried them all and they all worked perfectly.

This will make it much easier now Smilie

Appreciate your help!
# 5  
Old 06-09-2014
You are welcome.

If we are picky, slightly better still would be (using GNU sed):
Code:
sed 's/\<./X\u&/g' file

Since \b means any word boundary and \< means only word boundaries at the beginning of a word, although both should work since there are no uppercase spaces or punctuation characters..

Last edited by Scrutinizer; 06-09-2014 at 12:26 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 06-12-2014
Thanks very much Scrutinizer! Appreciate that you explained the tags as well. I'm a total newbie to this stuff Smilie Cheers
# 7  
Old 08-23-2014
Hello,

One more solution, which may help too.

Code:
awk '
function getstring(var) {
num=toupper(substr($var,1,1)) substr($var,2);
print num
}
{for(i=1;i<=NF;i++){
getstring(i);
{if(i==NF-1)
ORS="\n"
else
ORS=" "}
}
}' ORS=" " filename

Output will be as follows.

Code:
This Is Line One
This Is Line Two
This Is Line Three

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-23-2014 at 04:17 AM.. Reason: Earlier post was not providing the result as requested
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert text between exact matching patterns to Title case

Hi Folks, I have a large text file with multiple similar patterns on each line like: blank">PATTERN1 some word PATTERN2 title=">PATTERN1 some word PATTERN2 blank">PATTERN1 another word PATTERN2 title=">PATTERN1 another word PATTERN2 blank">PATTERN1 one more time PATTERN2 title=">PATTERN1... (10 Replies)
Discussion started by: martinsmith
10 Replies

2. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

3. Shell Programming and Scripting

Perl - Title Case after apostrophe

I've got: $string =~ s/(\w+)/\u\L$1/g; Which capitalizes each word in the string. The problem is if I have a string with an apostrophe the first letter after it gets capitalized as well. So Bob's becomes Bob'S. Thanks for any quick fixes! (4 Replies)
Discussion started by: mjmtaiwan
4 Replies

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

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

6. Shell Programming and Scripting

Shell/Perl script to convert to Capitalize case

I need a shell script which will convert the given string within a <title> tag to Capitalize case. E.g "<title>hi man: check this out</title>" to "<title>Hi Man: Check This Out</title>" (11 Replies)
Discussion started by: parshant_bvcoe
11 Replies

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

8. Shell Programming and Scripting

script to convert words into links

hi, i need some help. i have to write a unix shell script that opens a file with a list of words and prints another file with a list of links. something like this: Input: FILE 1: Red Green Blue Output: FILE2: http://www.google.com/search?hl=en&q=Red... (9 Replies)
Discussion started by: kazordoon
9 Replies

9. UNIX for Advanced & Expert Users

Shell script to convert to Title case

I need a shell script which will convert the given string to Title case. E.g "hi man" to "Hi man" (5 Replies)
Discussion started by: SankarV
5 Replies

10. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question