TR : surprises me


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting TR : surprises me
# 1  
Old 06-29-2005
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
# 2  
Old 06-29-2005
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.
# 3  
Old 06-29-2005
Perderabo,

in that case, would this be more meaningful ?

Code:
echo "p-20050608-Ajyd" | tr "p-20050608-A'[[:lower]]'" "p-20050608-A'[[:upper]]'"

# 4  
Old 06-29-2005
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".
# 5  
Old 06-29-2005
Question

Smilie
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.
# 6  
Old 06-29-2005
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.
# 7  
Old 06-29-2005
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
}' |

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question