How to change the letters from upper case to lower case using sed command
In this part of writing, we will explain the use of sed command to change the letters from upper case to lower case in Ubuntu terminal:
Let’s start from the very basic use of sed command to change all letters from uppercase to lower case in a text file: we have a text file “upper.txt” and the content of the file shows that there is no lower-case letter in the file as can be seen below:
To change it, the command written below will change all its letters from upper case to lower case:
Note: It is a case sensitive action so while switching from upper case to lower case, you must write capital letter [A-Z].; otherwise, this command won’t work:
And if you want to change only few letters from upper to lower case then you have to specify the letters separated by comma “,”: For instance, the command stated below will change only, “S”, “D” and “U” letters to lower case.
How to change the letters from lower case to upper case using sed command
To change the case of letters from lower to upper, there is a slight difference between commands; We created a text file “lower.txt” that will be used in this section and the content of this file is displayed below:
For instance, the command written below will change all the lower-case letters to upper case letter in a text file:
Moreover, you have the option to capitalize few letters instead of changing the whole document; For example, the command written below will capitalize the letters “L”, “D”, and “F” in text file “lower.txt”;
Apart from these fundamental operations about upper case and lower case; sed command also allows you to capitalize only the first letter of each word in a document; this functionality can be very useful for employee record management. For instance, you have a list of first names and last names of your 500 employees; this sed command would help you in this regard to change the first letter of their names to capital. The syntax of the command is written below:
sed [options] 's/\b\(.\)/\u\1/g' [file name]
The command mentioned below shows the application of above stated syntax: For instance, we have a list of three names; each name consists of two words and we want to capitalize first letter of each word then:
The image below shows that firstly all the letters were in lower case, however, once the command is executed the first letter of each name was capitalized:
Conclusion
Linux Operating System is well known for its command line support and the distros of Linux also have the same reason of popularity. One of the most famous distro Ubuntu contains a large pool of command line utilities to automate several tasks, such as sed command is used widely to perform several actions on text files using terminal. The sed utility can be launched in terminal and used to edit text files with one line operation that will be applied on the whole file. Following the importance of this command, we have compiled this guide to demonstrate the ways of using sed command to change the case of letters in a text file and discussed the conversions of Upper to Lower case and vice versa.