Reputation: 20019
I want to emulate rev
command (on alphabet) with the tr
command
So I did this
echo abcdefghijklmnopqrstuvxyz | tr '[z-a]' '[a-z]'
That gives me
tr: range-endpoints of `z-a' are in reverse collating sequence order
So I tried
echo abcdefghijklmnopqrstuvxyz | tr 'abcdefghijklmnopqrstuvxyz' 'zyxvutsrqponmlkjihgfedcba'
and it works ok, output is zyxvutsrqponmlkjihgfedcba
That is wrong with tr '[z-a]' '[a-z]'
?
If I specify tr '[a-c]' '[c-a]'
it also works fine.
Any help would be appreciated
Thank you
Upvotes: 4
Views: 2881
Reputation: 37298
I think your lucky with the '[c-a]' thing, My tr says tr: [c-a]: invalid destination string
, which is what I would expect.
Character ranges, i.e. [a-z], in all the languages I've dealt with, need to ascend in value. Be happy that you've already figured out your solution.
Or write a function that you can use like tr "[a-z]" "[$( revCharRange a-z )]"
(which would be more expensive due to the sub-shells required to create the reverse character range.
Edit
where revCharRange a-z
would output zyxw...cba
I hope this helps.
Upvotes: 4