Trying to capitalize first letter of every word in Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to capitalize first letter of every word in Variable
# 1  
Old 04-20-2010
Trying to capitalize first letter of every word in Variable

Total Bash noob, have been successful in doing my script by searching and looking at examples, but I need some assitance with this one, just can't figure it out.

In the Bash script I am trying to capitalize the first letter of every word in a string, ideally not changing other capitalization.

An example:
StartingVariable="NZ is the code for New Zealand"
NewVariable="NZ Is The Code For New Zealand"

If the only choice is title case then I'll go that route which would result NZ becoming Nz. Ultimately I can't figure out how to do either.

I did find a similar discussion here:
[SOLVED] 'tr' command working in console but not in bash script - Ubuntu Forums
And while it did echo the Title Case to the terminal, I couldn't figure out how to set a variable to that output.

I would really appreciate some help with this.

Thanks.

Randy
# 2  
Old 04-20-2010
Code:
echo 'NZ is the code for New Zealand' |  nawk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'

# 3  
Old 04-20-2010
Uh oh, I just tried this and got a message: nawk: command not found.

I didn't know it would make a difference, but I am doing this on a Mac running Snow Leopard. [whoops]

Ah, on a lark I changed nawk to awk and it works!

Fantastic, only I'm not sure how to set a new variable to that output.

Code:
VAR="NZ is the code for New Zealand"
VARCAP=${nawk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'}

something like that I imagine.

Last edited by vgersh99; 04-20-2010 at 04:34 PM.. Reason: code tags, please!
# 4  
Old 04-20-2010
Quote:
Originally Posted by randyharris
Uh oh, I just tried this and got a message: nawk: command not found.

I didn't know it would make a difference, but I am doing this on a Mac running Snow Leopard. [whoops]

Ah, on a lark I changed nawk to awk and it works!

Fantastic, only I'm not sure how to set a new variable to that output.

Code:
VAR="NZ is the code for New Zealand"
VARCAP=${nawk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'}

something like that I imagine.
Code:
#!/bin/ksh

VAR="NZ is the code for New Zealand"
VARCAP=$(echo "${VAR}" | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'})

# 5  
Old 04-20-2010
AMAZING, Thanks so much for the help!


btw - for anybody else who this may help, there was one character which needed to be removed, the "}" the second to last character.

This works beautiful for me:

Code:
#! /bin/bash

VAR="NZ is the code for New Zealand"
VARCAP=$( echo "${VAR}" | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')

echo "VAR: $VAR"
echo "VARCAP: $VARCAP"

# 6  
Old 04-20-2010
sorry - fat fingers.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing word and Capitalize words after

I have an assignment and I am not sure what to do. In Unix, I use PuTTY change the semicolon (;) to a period, and capitalize the first letter of the word immediately after it. I know change command is M-% and "." so only one semicolon is changed but I am not sure how to... (1 Reply)
Discussion started by: kathrut43
1 Replies

2. Shell Programming and Scripting

Setting letter as variable

I need to pass letter like "c" for "-copy" and need to read by variable $sel_mod, My I know the good way of writing the script. Example: Select one of the Clone Mode ( '-copy', '-differential', '-precopy', '-nopcopy') Type the Clone mode : c Thanks in advance, Ashan ... (6 Replies)
Discussion started by: ashanabey
6 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. Shell Programming and Scripting

Get letter from number and assign to a variable

Hi to all in forum, I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:LetterNumber=10 printf "\x$(printf %x $((${LetterNumber}+64)))" $ J #The problem, how to assign printf output (J in this... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

5. Shell Programming and Scripting

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... (16 Replies)
Discussion started by: cgkmal
16 Replies

6. Shell Programming and Scripting

Set the last 4 letter in the filename as a variable

Hi all, I want to set the last 4 letter in the filename as a variable. For example, i have AB1234.txt file and i need to have last 4 letter as a variable. It should be like ; get last four letter set var = 1234 How can i write this in C shell?? Thanks, zibi (3 Replies)
Discussion started by: zibi
3 Replies

7. Shell Programming and Scripting

Taking letter from a word

Hi All, I am new to UNIX and i am trying to write a script.My requirement is that from the following logs i need to get the following outputs: abc_lifecycle.log bcde_enjoy.log abc_twinkle.log Output expecting: lifecycle enjoy twinkle Could you please help me in getting this? (9 Replies)
Discussion started by: JeiPrakash
9 Replies

8. Shell Programming and Scripting

matching a letter in a word

hi, if i have a string of letters and seperatly i have a single letter. how do i check whether that specific letter is in my string aswell? any ideas? (2 Replies)
Discussion started by: Furqan_79
2 Replies

9. Shell Programming and Scripting

First letter of each Word from a line

Hello All, I am new to UNIX, how do i get the First letter of each Word from a line in shell scripting. For Example line = "The Jack In The Box" I want to reterive The letters T for The, J from Jack, I from In, T from The and B from Box. and store in another string. Can anyone... (5 Replies)
Discussion started by: maxmave
5 Replies

10. UNIX for Advanced & Expert Users

How to filter the words, if that word contains the expected letter

Hi, I am trying to filter the words from a file which contain 'abc'. But I am unable to. Could any one help me. For eg: The file contents are 123ab 12hnj1 123abc456 123cgbcahjkf23 23134abchfhj43 gc32abc abc1 2abc3 sd uiguif fhwe 21242 uh123 jkcas124d123 u3hdbh23u ffsd8 Output... (3 Replies)
Discussion started by: venu_eie
3 Replies
Login or Register to Ask a Question