Convert text files to all upper or lower case

As usual, in Linux, there are more than ONE 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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s