![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
||||
|
||||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
||||
|
||||
|
Perderabo,
in that case, would this be more meaningful ? Code:
echo "p-20050608-Ajyd" | tr "p-20050608-A'[[:lower]]'" "p-20050608-A'[[:upper]]'" |
|
#4
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
}' |
|
|||
| Google The UNIX and Linux Forums |