HOWTO convert text between uppercase and lowercase from the command line

From LinuxReviews
Jump to navigationJump to search
Terminal.png

Linux distributions come with several tools which lets you easily convert text between uppercase and lowercase from the command line or a shell script. Here's how you can do it using tr, awk and sed.

Using tr (translate or delete characters)

The command line tool tr can modify text in a number of ways. Converting text between lower and uppercase is one of its many built-in features. tr is meant to be used with pipes, you can pipe text to tr using cat or echo and have it manipulate the text for you. tr has two mandatory arguments, the text which should be converted and the target text. The special words [:lower:] and [:upper:] can be used to change lowercase text to uppercase.

echo hello | tr [:lower:] [:upper:]

will produce the output HELLO. echo hello | tr a-z A-Z will produce the same output.

Do note that tr [:lower:] [:upper:] will not work with some native language characters. echo bodø | tr [:lower:] [:upper:] will output BODø - not BODØ as Norwegian people would expect.

Flip the order of the [:lower:] and [:upper:] arguments to get the opposite effect, echo HELLO | tr [:upper:] [:lower:] will say hello.

The tr manual page[1] lists quite a few other options. tr is a part of GNU coreutils.

Using (g)awk (pattern scanning and processing language)

awk is a programming language written with pattern processing in mind. Linux distributions will typically have the GNU projects implementation installed and have awk symbolically linked to gawk. Converting between uppercase and lowercase is one of the many things you can with awk.

Converting lowercase to uppercase can be done by awk '{print toupper($0)}' and the opposite can be done with awk '{print tolower($0)}':

echo hello | awk '{print toupper($0)}'

will produce HELLO and

echo HELLO | awk '{print tolower($0)}'

will produce hello.

awk handles international characters correctly. echo bodø | awk '{print toupper($0)}' outputs BODØ as you would expect (tr [:lower:] [:upper:] does not).

awk can be used for much, much more. The manual page [2] is very long and there are also many thick books about awk available.

Using sed (stream editor)

sed is a really powerful stream editor which can be used to filter and manipulate text in all sorts of ways. There is, naturally, a way to convert between uppercase and lowercase using sed - just like it can do pretty much everything else (if you know how).

Sed is typically used with something to replace followed by something to replace that with separated by either / or #. [a-z] can be used to signal that lowercase text should me manipulated, [A-Z] can be used to mark uppercase text. \U& and \L& can be used to convert to uppercase and lowercase respectively.

echo hello | sed 's/[a-z]/\U&/g'

will produce HELLO and

echo HeLlO | sed 's/[A-Z]/\L&/g'

results in hello. This works with international characters as well:

echo BODØ | sed 's/[A-Z]/\L&/g' will output bodø (even though ø comes after z in the alphabet).

sed can be used for much more. It is really powerful if you know regex. General basic use is as simple as echo this | sed s/this/that/g yet the possibilities it offers are vast. The manual page[3] has more detail.

Using tr, awk or sed to change a text file

awk, sed and tr will assume that a last argument given to them is a text file and open it. They will assume pipes are to be used if no filename is specified.

echo hello > text.txt
awk '{print toupper($0)}' text.txt

will first write hello to text.txt and open text.txt in awk - which will output HELLO. Changing the awk line to

awk '{print toupper($0)}' text.txt > uppertext.txt

would produce a file called uppertext.txt containing <cocde>HELLO.

Converting the first letter of a word to uppercase

sed can do this with sed "s/\b\(.\)/\u\1/g"

echo hello world | sed "s/\b\(.\)/\u\1/g"

produces Hello World

This method is obviously not ideal if you want to change a large text file since it makes the first letter of all words uppercase.

The above command lacks one function you may want: If does nothing beyond changing the first letter of each word to uppercase. A far more advanced sed command is required if you want to make sure the first letter of each word is the only word in uppercase. sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g' will do the trick,

echo hElLo wOrLD | sed 's/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g'

will output Hello World

Footnotes

  1. man.linuxreviews.org: tr.1
  2. man.linuxreviews.org: gawk.1
  3. man.linuxreviews.org: sed.1

Questions?

Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.