Converting between uppercase and lowercase on the Linux command line
Using awk
The awk command lets you do the same thing with its toupper and tolower options. The command in the script shown in the previous example could be done this way instead:
echo $dept | awk '{print toupper($0)}' >> depts
The reverse (switching to lowercase) would look like this:
echo $dept | awk '{print tolower($0)}' >> depts
Using sed
The sed (stream editor) command also does a great job of switching between upper- and lowercase. This command would have the same effect as the first of the two shown above.
echo $dept | sed 's/[a-z]/U&/g' >> depts
Switching from uppercase to lowercase would simply involve replacing the U near the end of the line with an L.
echo $dept | sed 's/[A-Z]/L&/g' >> depts
Manipulating text in a file
Both awk and sed also allow you to change the case of text for entire files. So, you just found out your boss wanted those department names in all lowercase? No problem. Just run a command like this with the file name provided:
$ awk '{print tolower($0)}' depts finance billing bookkeeping
If you want to overwrite the depts file, instead of just displaying its contents in lowercase, you would need to do something like this: