Search This Blog

Loading...

Sunday, May 11, 2008

How to convert text files to all upper or lower case

How can you convert a text file to all lower case or all upper case?

As usual, in Linux, there are more than 1 way to accomplish a task.

To convert a file (input.txt) to all lower case (output.txt), choose any ONE of the following:

  • dd
    $ dd if=input.txt of=output.txt conv=lcase

  • tr
    $ tr '[:upper:]' '[:lower:]' < input.txt > output.txt

  • awk
    $ awk '{ print tolower($0) }' input.txt > output.txt

  • perl
    $ perl -pe '$_= lc($_)' input.txt > output.txt

  • sed
    $ sed -e 's/\(.*\)/\L\1/' input.txt > output.txt

    We use the backreference \1 to refer to the entire line and the \L to convert to lower case.


To convert a file (input.txt) to all upper case (output.txt):

  • dd
    $ dd if=input.txt of=output.txt conv=ucase

  • tr
    $ tr '[:lower:]' '[:upper:]' < input.txt > output.txt

  • awk
    $ awk '{ print toupper($0) }' input.txt > output.txt

  • perl
    $ perl -pe '$_= uc($_)' input.txt > output.txt

  • sed
    $ sed -e 's/\(.*\)/\U\1/' input.txt > output.txt




StumbleUpon Toolbar

9 comments:

Anonymous said...

nice :-) . may come in handy. thanx.

biggnou said...

thanks, always great to learn about sed ;)

The dd one surprised me, but that's linux ! Always surprising !

viztro said...

when converting accented vowels

some conversions fail but this one work

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

Mak @ unix and linux shell script forum said...

Thanks for the useful article on tr command. More examples at
tr command example in unix

naisanza said...

There's always something more to learn about sed. Thanks!

Anonymous said...

Awesome!!!!
tr function is great...:)

Anonymous said...

can we use cat command to convert text???

Unknown said...

You can alway use text-converter.com

Ganesh Pathak said...

good article

it could be more easy with

#tr "a-z" "A-Z" < input.file