How to Use sed to Convert All Characters to Lowercase


Often you may want to use sed to convert all characters in a string to lowercase.

You can use the following basic syntax to do so:

sed -e 's/.*/\L&/' cities.txt

This particular example will convert each character in each line of the file named cities.txt to lowercase.

Here is what the various sed commands do:

  • s/: Used to replace a specific pattern with another pattern
  • .*: Used to specify that all characters in each line should be replaced
  • \L&: Used to specify that all characters in lowercase from each line should be used as replacements

The following example shows how to use this syntax in practice.

Example: How to Use sed to Convert All Characters to Lowercase

Suppose we have a file named cities.txt that contains the names of various U.S. cities:

Suppose that we would like to convert all of the characters in each city names to lowercase.

We can use the following syntax to do so:

sed -e 's/.*/\L&/' cities.txt 

The following screenshot shows how to use this syntax in practice:

sed convert to lowercase

Notice that this returns each city name in all lowercase letters.

Note that we could also use the > command to save these results to a new file named cities_new.txt:

sed -e 's/.*/\L&/' cities.txt > cities_new.txt

The following screenshot shows how to use this syntax in practice:

sed convert characters to lowercase and save to new file

Notice that when we use the cat command to view the contents of cities_new.txt that each city name in the file is shown in all lowercase letters.

Related Tutorials

The following tutorials explain how to perform other common tasks in sed:

How to Use sed to Replace All Text After Match
How to Use sed to Replace All Text Before Match
How to Use sed to Extract All Lines Containing a Pattern

Leave a Reply