Problem with tr command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with tr command
# 1  
Old 06-07-2012
Problem with tr command

Hi friends,
Today I found one strange behaviour of the tr command.
I used the following command:
Code:
echo "NEE"|tr [A-Z] [a-z]

Sometimes it was giving "nee" as ouput . sometimes it was giving "NEE" as output.
Finally I used the below code:
Code:
echo "NEE"|tr "[A-Z]" "[a-z]"

... and it gave me the correct result at every hit. I just want to know the reason behind this strange behaviour. Is it a problem of my Shell ? I am working in ksh88 version. Please help.

Last edited by methyl; 06-08-2012 at 04:35 PM.. Reason: spellcheck and grammar check
# 2  
Old 06-07-2012
I can't really speak to the inconsistent output using "[A-Z] [a-z]", however I can say that I have never seen that using this method to go from upper to lower...

Code:
echo "NEE"|tr [:upper:] [:lower:]

# 3  
Old 06-07-2012
Quote:
echo "NEE"|tr [A-Z] [a-z]
This should be a syntax error. You must have a funny O/S if it works at all.

The correct syntax is:
Code:
echo "NEE" | tr '[A-Z]' '[a-z]'

... or to paraphrase man tr
Code:
echo "NEE" | tr "[:upper:]" "[:lower:]"

... but I prefer single quotes to make it totally obvious that the parameters are for tr not Shell.
Code:
echo "NEE" | tr '[:upper:]' '[:lower:]'



Btw. I've seen some amazing anomolies if there are files with single character names in the current directory.
# 4  
Old 06-07-2012
Hmm

man tr on my servers do not show the need for a single or double quote.

I am using RHEL 5.3 and have been using the "[:upper:] [:lower:]" notation for years without quoting them. In fact we have a handful of Solaris and even HP-UX servers and many of our common scripts use that notation.

What OS are you using?
What version?
What version of tr?

It would be good to remember that quoting is required on that OS/Version or more importantly that version of tr.

Last edited by methyl; 06-08-2012 at 04:31 PM.. Reason: spellcheck and grammar check
# 5  
Old 06-07-2012
@neelmani
Please post what Operating System and version you are running and what Shell you use.
(Prediction: The O/S going to be something unusual).


@ddreggors
Linux syntax can be wildly and inconsistently different from unix syntax and from other Linux systems syntax.
The unquoted syntax posted by the O/P is a syntax error on HP-UX.
There are much too many many Scripts running on live systems which contain faulty tr commands.
Please post your syntax and context.

Last edited by methyl; 06-07-2012 at 05:51 PM..
# 6  
Old 06-07-2012
Quote:
Originally Posted by methyl
This should be a syntax error. You must have a funny O/S if it works at all.

The correct syntax is:
Code:
echo "NEE" | tr '[A-Z]' '[a-z]'

Unlike character classes, tr ranges are not delimited by square brackets. tr '[A-Z]' '[a-z]' is converting [ and ] into themselves, a silent, harmless error.

The unquoted version could trigger pathname expansion in the shell, but I don't know why it would give you a syntax error. Could you please elaborate? Is it a tr syntax error? A shell error?

I think the original problem is as you alluded, the result of the shell matching single letter pathnames against the unquoted [a-z] and [A-Z]. This could alter the arguments to tr in such a way that none of the letters in NEE match the first range and so are never converted.

I would suggest trying tr A-Z a-z (valid only in the c/posix locale).

Regards,
Alister

---------- Post updated at 06:02 PM ---------- Previous update was at 05:53 PM ----------

Quote:
Originally Posted by ddreggors
hmm

man tr on my servers do not show the need for a singe or double quote.

I am using RHEL 5.3 and have been using the "[:upper:] [:lower:]" notation for years without quoting them. In fact we have a handful of Solaris and even HPUX servers and many of our common scripts use that notation.

What OS are you using?
What version?
What version of tr?

It would be good to remember that quoting is required on that OS/Version or more importantly that version of tr.
It's not tr that requires the quoting; it's the shell (any posix-like sh). [:upper:] and [:lower:] are valid pathname expansion operations in the shell, each matching a single character name. If there's a match, instead of tr seeing a character class it will see the single character name with which the shell replaced the original character class (which to the shell was a pattern for globbing). If there's no match (probably usually the case, which is why you haven't noticed a problem), the shell doesn't modify the pattern and tr is invoked as intended.

Not to put a fine point on it, but all of those scripts have a bug and should be quoted.

Regards,
Alister

Last edited by alister; 06-07-2012 at 07:16 PM..
This User Gave Thanks to alister For This Post:
# 7  
Old 06-07-2012
Thanks alister, that is good to know. I have been using tr for years with that syntax and just never have seen any problems. It had always performed as expected. When you mention the shell globbing it makes a lot more sense. I guess as you said, I have just been lucky Smilie

Admittedly, I use tr less often than say grep, awk, or sed... but enough that this is valuable to know.

Last edited by methyl; 06-08-2012 at 04:31 PM.. Reason: spellcheck and grammar check
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

I'm facing problem with rpm command, when running the command and appears this error:

exec(): 0509-036 Cannot load program /usr/opt/freeware/bin/rpm because of the following errors: 0509-022 Cannot load module /opt/freeware/lib/libintl.a(libintl.so.1). 0509-150 Dependent module /opt/freeware/lib/libiconv.a(shr4.o) could not be loaded. 0509-152 Member... (4 Replies)
Discussion started by: Ohmkar
4 Replies

2. Shell Programming and Scripting

Problem with mv command and touch command

Hi guys, first of all I would say that this is my first time I write in a Forum. I've read the "forum rules" and I hope i will respect them. I searched everywhere for the solution of my problem but I didn't find anything. Here my problem: I'm using a sap job scheduler: in a particular job... (7 Replies)
Discussion started by: Antcam
7 Replies

3. Red Hat

please help me in if command problem

Please help me this script !!!! Script ***** a=2.0.0.0 b=1.0.0.0 #if test $a -ge $b if then echo "A is Greater than B" else echo "B is Greater than A" fi i am getting following error >sh abc abc: line 6: [: 2.0.0.0: integer expression expected (8 Replies)
Discussion started by: ponmuthu
8 Replies

4. Shell Programming and Scripting

Problem with command tr

Hello, excuse me for my english, i'm a french man. I have a problem with the command tr in applescript (with the accent...in french we have a lot of accents), i have read that is a problem with the version 10.5 of leopard then i would want to download the version 10.4 of universal binary of tr... (1 Reply)
Discussion started by: protocomm
1 Replies

5. Solaris

problem with ps command

Hi all I use to run sql loader inside a script with usename and password written in syntax. Now when I use 'ps' command to know about the status of loading, it also shows username and password that I don't want to share with someone who is sitting with me. If you have any idea except encryption... (1 Reply)
Discussion started by: sanjay1979
1 Replies

6. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

7. Shell Programming and Scripting

problem with dd command or maybe AFS problem

Hi, folks. Sorry for bothering, but maybe someone could help me please. The problem is the following: there is some script that copies files from local file system to AFS. The copying is performed with dd command. The script copies data into some AFS volumes. The problem appeared with one... (0 Replies)
Discussion started by: Anta
0 Replies

8. UNIX for Dummies Questions & Answers

Problem with ps command??

I have a problem to.... (1).List the number of unique users that has active processes. (2).List the number of active processes for each of the users in (1). (3).Determine total memory usage for each user. Help me please....thank you. Best... (2 Replies)
Discussion started by: robocup
2 Replies

9. UNIX for Dummies Questions & Answers

problem with who command

Sorry for my poor english. Unix is SCO ODT ver 3.0 Mine problem is : when I login via some terminal emulator and type : who am i I see information like this : username ttyp02 Feb 28 09:53 after logoff and type who command (from some other terminal) I see that ttyp02 is still... (2 Replies)
Discussion started by: bane_yu
2 Replies
Login or Register to Ask a Question