Tuesday, March 13, 2012

Different ways to capitalize the contents of a file




   In this article, we will see the different ways in which we can capitalize the contents of the file and also the other way around. In other words, this will tell how to convert the lowercase characters or string into upper case and upper case into lower case.

Let us take a sample file, "file1", with the following contents:
$ cat file1
hello
welcome
string

1. awk provides a function, "toupper", which capitalizes the string given to it.
$ awk '{print toupper($0)}' file1
HELLO
WELCOME
STRING
 To convert upper case to lower case, use "tolower" in place of "toupper".

2. In GNU sed, there is a switch, \U, which converts the matched string to uppercase. we match the entire string and use \U on it.
$ sed 's/.*/\U&/' file1
HELLO
WELCOME
STRING
 To convert upper case to lower case, use "\L" in place of "\U".

3. tr is one of the commonly used functions for converting between lowercase to uppercase and vice versa.
$ tr '[:lower:]' '[:upper:]' < file1
HELLO
WELCOME
STRING
Other way of doing the same using tr is:
$ tr '[a-z]' '[A-Z]' < file1
HELLO
WELCOME
STRING
  To convert upper case to lower case, just swap the positions of [:lower:] and [:upper:].

4. Perl provides a function with the name "uc" to do the lowercase to uppercase conversion.
$ perl -ne 'print uc;' file1
HELLO
WELCOME
STRING
To convert upper case to lower case,  use "lc" in place of "uc".


5. In Ksh(k shell), the typeset command comes with -u option. In one of our earlier articles, we learnt about the typeset command and its usage in arithmetic. typeset is a command used to declare a variable in shell. The "-u" option declares the variable as an uppercase variable. This means any string assigned to this variable will automatically be converted to uppercase and stored.
$ typeset -u X
$ X='welcome'
$ echo $X
WELCOME
To convert upper case to lower case,  use "typeset -l" in place of "typeset -u". "-l" indicates lower case.


6. At times, you might be typing something at the command prompt. You typed it small and you want to capitalize it. Its pretty simple if you are in bash shell.

  Assuming at the bash prompt, you typed "welcome"
$ welcome
   To convert to uppercase, do the following:
  1. Esc-b (escape + b) # Takes cursor to beginning of the word.
  2. Esc-u (escape + u) # Capitalizes the word.

To convert upper case to lower case,  use 'Esc-l' in place of 'Esc-u'.

7. When you are in the vim editor, whilel typing something, you may want to capitalize a certain text which you typed. The visual mode of vim editor is a big advantage here. Do the following to check this:
  1. Open the vim editor.
  2. Type a word, say "welcome"
  3. Go back to the escape mode(Press escape).
  The remaining 3 steps to be done in escape mode:
  4. 0 (Press 0. Goes to the beginning of the word)
  5. ve (Give the command "ve" . The word gets highlighted)
  6. U (Give the command "U". This capitalizes the word.)

   The same way you can capitalize an entire line or the entire file by choosing the content in the visual mode.

To convert upper case to lower case,  in the Step 6, in place of 'U', use 'u'.


No comments:

Post a Comment