Operating System - HP-UX
1833876 Members
1871 Online
110063 Solutions
New Discussion

Upper Case - Lower Case Question -

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

Upper Case - Lower Case Question -

I have a file that contains a number of entries that are in upper case and others are in lower case (see example below). How can I use a unix command(s) to change all entries to lower case?

hostname1.domain.com
HOSTNAME2.domain.com
HOSTNAME3.domain.com
hostname4.domain.com
HOSTNAME5.domain.com
10 REPLIES 10
Leif Halvarsson_2
Honored Contributor
Solution

Re: Upper Case - Lower Case Question -

Hi,

cat | tr [:upper:] [:lower:]

See the man page for tr
Pete Randall
Outstanding Contributor

Re: Upper Case - Lower Case Question -


tr "[:lower:]" "[:upper:]" file2


Pete

Pete
Pete Randall
Outstanding Contributor

Re: Upper Case - Lower Case Question -

Sorry - that was backwards - try this:

tr "[:upper:]" "[:lower:]" file2


Pete

Pete
Rob Johnson_3
Regular Advisor

Re: Upper Case - Lower Case Question -

I did
cat file|tr "[:upper:]" "[:lower:]" > file1
and it works fine.

I love this forum. Thanks to all of you, I always get a quick, accurate response to my posts!!!!!!!!!!!!!!!!!!!!!

Cem Tugrul
Esteemed Contributor

Re: Upper Case - Lower Case Question -

Rob,

As an additon to other replies another fantastic links which may help you;

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=694878

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=840763

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=87241

Good luck,
Our greatest duty in this life is to help others. And please, if you can't
Sandman!
Honored Contributor

Re: Upper Case - Lower Case Question -

Yet another way to do the same would be to use an awk construct like:

# awk '{print tolower($0)}'


Sandman!
Honored Contributor

Re: Upper Case - Lower Case Question -

And since you would like to save your results into an output file...

# awk '{print tolower($0)}' input_file > output_file

Also can be done from within ex...no output file needed as ex will modify the exisiting one.

# ex -s input_file <> %s/.*/\L&/
> x
> EOF

regards
Jack C. Mahaffey
Super Advisor

Re: Upper Case - Lower Case Question -

I use a script to do the work.

Here's the script.
#!/usr/bin/sh
VAR=$*
LOWER=`echo $VAR | tr "[A-Z]" "[a-z]" `
echo $LOWER
exit 0

Example:
mystringlower=`lowercase.sh $1`

Here's a script for upper case:
#!/usr/bin/sh
VAR=$*
UPPER=`echo $VAR | tr "[a-z]" "[A-Z]" `
echo $UPPER
exit 0

Jack C. Mahaffey
Super Advisor

Re: Upper Case - Lower Case Question -

I just re-read your post and my scripts are not applicable for converting a file. Sorry. The other solutions will work.

jack...
Raj D.
Honored Contributor

Re: Upper Case - Lower Case Question -

Hi Rob ,

assuming your file name is file1.txt

# tr "[:upper:]" "[:lower"] < file1.txt > file2.txt

check :
# cat file2.txt

hostname1.domain.com
hostname2.domain.com
hostname3.domain.com
hostname4.domain.com
hostname5.domain.com

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "