Different behavour with "tr" | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Different behavour with "tr"

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
jville jville is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 6 March 2014, 12:50 PM EST
Posts: 95
Thanks: 9
Thanked 8 Times in 8 Posts
Different behavour with "tr"

I am getting different output with the 'tr' command in bash
I am using
Linux:
Red Hat Enterprise Linux Server release 5.3 (Tikanga)
Bash version:
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)


Code:
$ x=`echo XXx | tr '[a-z]' '[A-Z]' `
$ echo $x
XXX

$ y=`echo XXx | tr [:lower:] [:upper:] `
$ echo $y
XXx

Sponsored Links
    #2  
Old 12-05-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 April 2014, 1:43 PM EDT
Location: Saskatchewan
Posts: 18,538
Thanks: 682
Thanked 3,036 Times in 2,856 Posts
Probably a character-set thing. The GNU utilities try to abide by your character set whenever possible, and UTF8 is increasingly standard, but the definition of :lower: and :upper: for UTF8 is going to be pretty big and complicated.

Other side-effects of this compliance are things like slow-performing grep -i.

Try LOCALE="C" tr ... and see if it works as expected.

Also, if you have a file in your directory named :, l, o, w, e, r, u, or p, [:lower:] and [:upper:] will actually glob those filenames, so put them in single quotes! '[:lower:]' '[:upper:]'
The Following User Says Thank You to Corona688 For This Useful Post:
jville (12-05-2012)
Sponsored Links
    #3  
Old 12-05-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Last Activity: 24 April 2014, 6:44 AM EDT
Posts: 3,132
Thanks: 172
Thanked 936 Times in 761 Posts
Quote:
Originally Posted by Corona688 View Post
Probably a character-set thing.

...<snip>...

Also, if you have a file in your directory named :, l, o, w, e, r, u, or p, [:lower:] and [:upper:] will actually glob those filenames, so put them in single quotes! '[:lower:]' '[:upper:]'
I believe the globbing is the problem. If [:lower:] matches a filename, the lower case x won't appear in the first argument to tr and will not be converted.

The issue with locales which interleave upper and lower case letters (such as english utf-8) typically manifests as unintended conversions.

I would suggest, as you did, to quote the arguments to tr. For confirmation, set -x .

Regards,
Alister
The Following User Says Thank You to alister For This Useful Post:
jville (12-05-2012)
    #4  
Old 12-05-2012
jville jville is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 6 March 2014, 12:50 PM EST
Posts: 95
Thanks: 9
Thanked 8 Times in 8 Posts
that explains it.
globbing was the culprit in this case. I had a file with 'w' in directory where I was executing this command. Thanks for your help !
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
The slices "usr", "opt", "tmp" disappeared!!! Help please. wolfgang Solaris 16 05-25-2012 08:09 AM
how to use "cut" or "awk" or "sed" to remove a string timmywong Shell Programming and Scripting 8 02-12-2012 11:55 AM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM



All times are GMT -4. The time now is 07:25 AM.