Shell Scripting – How to Convert File Contents to Lower Case

sedshelltcshtext processingtr

I have temp file with some lower-case and upper-case contents.

Input

Contents of my temp file:

hi
Jigar
GANDHI
jiga

I want to convert all upper to lower.

Command

I tried the following command:

sed -e "s/[A-Z]/[a-z]/g" temp

but got wrong output.

Output

I want it as:

hi
jigar
gandhi
jiga

What needs to be in the substitute part of argument for sed?

Best Answer

If your input only contains ASCII characters, you could use tr like:

tr A-Z a-z < input 

or (less easy to remember and type IMO; but not limited to ASCII latin letters, though in some implementations including GNU tr, still limited to single-byte characters, so in UTF-8 locales, still limited to ASCII letters):

tr '[:upper:]' '[:lower:]' < input

if you have to use sed:

sed 's/.*/\L&/g' < input

(here assuming the GNU implementation).

With POSIX sed, you'd need to specify all the transliterations and then you can choose which letters you want to convert:

sed 'y/AǼBCΓDEFGH.../aǽbcγdefgh.../' < input

With awk:

awk '{print tolower($0)}' < input
Related Question