HOWTO - change letter case of variable value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HOWTO - change letter case of variable value
# 1  
Old 06-08-2010
HOWTO - change letter case of variable value

Hi

I have e.g. VAR1=january and I need to change it into VAR1=January.

How to change value of VAR1 variable to always set first character uppercase and other lowercase ?

thx for help.
# 2  
Old 06-08-2010
Code:
echo "$var" | tr -s '[:lower:]'  '[:upper:]' | read tmp
var=$tmp

# 3  
Old 06-08-2010
Hi

Code:
echo hola | awk '{OFS="";print toupper(substr($1,1,1)),substr($1,2)}'

# 4  
Old 06-08-2010
try this one
Code:
echo hello | sed 's/.*/\u&/'

# 5  
Old 06-08-2010
Question

I used advice from jim mcnamara, so :
Code:
echo "$var" | tr -s '[:lower:]'  '[:upper:]' | read tmp
var=$tmp

But when I call function which uses this mechanism I get such error :
./filterEngine.ksh[11]: February: not found

# cat filterEngine.ksh
Code:
#!/bin/ksh
################# Funtion monthGrep() #####################
   494  monthGrep()
   495  {
   496  echo "$Month" | tr -s '[:lower:]'  '[:upper:]' | read tmp
   497  Month="$tmp"
   498  print $Month
   499
   500  case $Month in
   501          STYCZEN)
   502                 `print January`
   503                  ;;
   504          LUTY)
   505                  `print "February"`
   506                  ;;
   507          MARZEC)
   508                  `print March`
   509                  ;;
   510          KWIECIEN)
   511                  `print April`
   512                  ;;
   513          MAJ)
   514                  `print May`
   515                  ;;
   516          CZERWIEC)
   517                  `print June`
   518                  ;;
   519          LIPIEC)
   520                  `print July`
   521                  ;;
   522          SIERPIEN)
   523                  `print August`
   524                  ;;
   525          WRZESIEN)
   526                  `print Septemebr`
   527                  ;;
   528          PAZDZIERNIK)
   529                  `print October`
   530                  ;;
   531          LISTOPAD)
   532                  `print November`
   533                  ;;
   534          GRUDZIEN)
   535                  `print December`
   536                  ;;
   537          *)
   538                  `print /dev/null`
   539                  ;;
   540  esac
}
#############################################################
print -n " What is the name of the month? "
read Month

monthGrep `print $Month`

thx for help
# 6  
Old 06-08-2010
Hello,

other solution that could be used for other languages is:

Create a file names (i.e) months with the content:
Code:
styczen:January
luty:February
marzec:March
...and so on...

then, if in the variable month you have the user entered, you can:

Code:
grep -i $month months | cut -d: -f 2

and the result will be the english name...
# 7  
Old 06-08-2010
bash 4
Code:
# name=january
# echo ${name^}
January

or..
Code:
# name=jAnUaRy
# first=${name:0:1}
# rest=${name:1}
# name=${first^}${rest,,}
# echo $name
January

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

toupper or tolower case of first letter of the line depending on another file

Hi I would like to read if the first letter of a line in a first file (gauche.txt) is uppercase or lowercase, and change consequently the first letter of the corresponding line in the second file (droiteInit.txt). I have done this but it won't work (I launch this using gawk -f... (16 Replies)
Discussion started by: louisJ
16 Replies

4. Shell Programming and Scripting

change each letter of a string

Is there a way to change each letter of a string to the next one in the alphabet, so that a becomes b and f becomes g, and digits become one unit bigger - 4 becomes 5 and 9 becomes 0. I want to change strings like ben123 to cfo234. (5 Replies)
Discussion started by: locoroco
5 Replies

5. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

6. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

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

8. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 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

Covert case of first letter only

Using sed or tr, how do I change "fred" into "Fred" or "fReD" into "Fred" (1 Reply)
Discussion started by: tisons
1 Replies
Login or Register to Ask a Question