How to convert lower case to upper case letters in a shell script/command How to convert lower case to upper case letters in a shell script/command, convert lower case to upper case, convert upper case to lower case
Here are a few tricks that can help when one needs to convert lower case to upper case characters in a file or in the ouput of a command.
I will start with the following example file:
$ cat lower-upper-case.file first lower case characters line second lower case characters line FIRST UPPER CASE CHARACTERS LINE SECOND UPPER CASE CHARACTERS LINE
Cases
Convert lower case to upper case using tr
$ tr '[a-z]' '[A-Z]' < lower-upper-case.file FIRST LOWER CASE CHARACTERS LINE SECOND LOWER CASE CHARACTERS LINE FIRST UPPER CASE CHARACTERS LINE SECOND UPPER CASE CHARACTERS LINE
The above example translates all lower-case chars to upper-case and writes the output to standard output.
Instead of tr '[a-z]' '[A-Z]' one can choose to use tr "[:lower:]" "[:upper:]" as in the following example:
$ tr "[:lower:]" "[:upper:]" < lower-upper-case.file FIRST LOWER CASE CHARACTERS LINE SECOND LOWER CASE CHARACTERS LINE FIRST UPPER CASE CHARACTERS LINE SECOND UPPER CASE CHARACTERS LINE
Convert upper case to lower case using tr
$ tr '[A-Z]' '[a-z]' < lower-upper-case.file first lower case characters line second lower case characters line first upper case characters line second upper case characters line
and for the second example:
$ tr "[:upper:]" "[:lower:]" < lower-upper-case.file first lower case characters line second lower case characters line first upper case characters line second upper case characters line
Convert upper case to lower case using dd (conv=lcase)
$ dd if=./lower-upper-case.file of=./lower-upper-case.file2 conv=lcase 0+1 records in 0+1 records out 134 bytes transferred in 0.000103 secs (1301011 bytes/sec) $ cat lower-upper-case.file2 first lower case characters line second lower case characters line first upper case characters line second upper case characters line
Convert lower case to upper case using dd (conv=ucase)
$ dd if=./lower-upper-case.file of=./lower-upper-case.file2 conv=ucase 0+1 records in 0+1 records out 134 bytes transferred in 0.000103 secs (1301011 bytes/sec) $ cat lower-upper-case.file2 FIRST LOWER CASE CHARACTERS LINE SECOND LOWER CASE CHARACTERS LINE FIRST UPPER CASE CHARACTERS LINE SECOND UPPER CASE CHARACTERS LINE
Both methods are very handy, but I'd say that using tr helps better inside scripts. It can redirect the output to a second file and using in further activities.
Designed and developed by Andrei Manescu. Optimized for Mozilla Firefox.
Copyright 2007 Andrei Manescu
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by those who posted them.