Pages

Friday, December 21, 2012

Shell script: Convert lower case to upper case

Q. I have a file which is of characters, I want to convert entire file content to upper case. How can I do that using a shell script?

This can be achieved by using tr or sed command. Below are two examples on do that.

#!/bin/bash
read -p "Please enter the file name whose content to be converted to upper case: " FILE1
tr '[a-z]' '[A-Z]' < $FILE1 >file2
mv file2 $FILE1
echo "Done the changes to file $FILE1"

Through sed command

#!/bin/bash

read -p "Please enter the file name whose content to be converted to upper case: " FILE1
sed -ir 's/(.*)/\U\1/g' $FILE1
echo "Done the changes to the file."

Do let us know if you know other way to do this.

For converting upper case to lower case use below scripts

with tr command

#!/bin/bash

read -p "Please enter the file name whose content to be converted to upper case: " FILE1
tr '[A-Z]' '[a-z]' < $FILE1 >file2
mv file2 $FILE1
echo "Done the changes to file $FILE1"

With SED command.

#!/bin/bash

read -p "Please enter the file name whose content to be converted to upper case: " FILE1
sed -ir 's/(.*)/\L\1/g' $FILE1
echo "Done the changes to the file."

Save the files execute them to get your files converted to upper/lower case.

0 comments:

Post a Comment