The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-29-2005
RishiPahuja's Avatar
Registered User
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Question TR : surprises me

I am not able to see why tr is behaving surprisingly strange.

I am pasting the commands and output. see if anyone can explain the mystery.

arbhvp02% echo "p-20050608-Ajyd-g.jpg" | tr "p-20050608-A'[a-z]'-g.jpg" "p-20050608-A'[A-Z]'-g.jpg"
p-20050608-AjYD-g.jpg
arbhvp02% echo "p-20050608-Ajjyd-g.jpg" | tr "p-20050608-A'[a-z]'-g.jpg" "p-20050608-A'[A-Z]'-g.jpg"
p-20050608-AjjYD-g.jpg
arbhvp02% echo "p-20050608-Axyd-g.jpg" | tr "p-20050608-A'[a-z]'-g.jpg" "p-20050608-A'[A-Z]'-g.jpg"
p-20050608-AXYD-g.jpg
arbhvp02% echo "p-20050608-Abyd-g.jpg" | tr "p-20050608-A'[a-z]'-g.jpg" "p-20050608-A'[A-Z]'-g.jpg"
p-20050608-ABYD-g.jpg
arbhvp02% echo "p-20050608-Abjyd-g.jpg" | tr "p-20050608-A'[a-z]'-g.jpg" "p-20050608-A'[A-Z]'-g.jpg"
p-20050608-ABjYD-g.jpg

Please see the bold parts, whenever I give j it is noever translated to upper case.
Want to know why it treats j so differently.

Regards,
Rishi
Reply With Quote
Forum Sponsor
  #2  
Old 06-29-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,609
tr does not match patterns. It works character by character. Both of args to tr end with "jpg". That means translate "j" to "j"; "p" to "p"; and "g" to "g". tr will never know that the string "jpg" appears in the data.
Reply With Quote
  #3  
Old 06-29-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,681
Perderabo,

in that case, would this be more meaningful ?

Code:
echo "p-20050608-Ajyd" | tr "p-20050608-A'[[:lower]]'" "p-20050608-A'[[:upper]]'"
Reply With Quote
  #4  
Old 06-29-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,609
Notice how the character zero appears more than once in the strings. That is a clear sign that you are still thinking in patterns. tr will never assemble 4 consecutive characters like "2005".
Reply With Quote
  #5  
Old 06-29-2005
RishiPahuja's Avatar
Registered User
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Question


Then in my particular case how to get my requirement done...
which is

If I have a string "p-20050608-Ajyd-g.jpg"
I want to convert a portion to uppercase only.
Reply With Quote
  #6  
Old 06-29-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,609
Basicly, break your string into 3 pieces, upsihift the middle one, and reassemble the whole string. How depends on what shell you're using. ksh can do everything with just shell builtins thanks to the magic of "typeset -u". With other shells it may be harder.
Reply With Quote
  #7  
Old 06-29-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
It should be quite easy in shell.
If you want speed though write a sed program

Here's a start...

Code:
echo "p-20050608-Ajyd-g.jpg" |
sed -e '
{
    #keep orig path in hold space
    h
                                                                                                                                                                                                                  
    #put 3rd bit in pattern space
    s/\(.*\)-\(.*\)-\(.*\)-\(.*\)/\3/
                                                                                                                                                                                                                  
    #lowercase
    y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
                                                                                                                                                                                                                  
    #exchange pattern and hold space
    x
                                                                                                                                                                                                                  
    #first bit
    s/\(.*\)-\(.*\)-\(.*\)-\(.*\)/\1-\2-\4/
                                                                                                                                                                                                                  
    #capitalised bit
    G
}' |
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0