Cannot translate specific characters with tr utility
Suppose I have a file named doc with content:
{}
[]
Now I want to translate all the '{' and '[' into '(' and '}' and ']' into ')'. So far, I can translate '{}' and '[' correctly, but I can't tra开发者_StackOverflownslate ']', using this shell command:
cat doc | tr [\[] [\(] | tr [\{] [\(] | tr [\}] [\)] | tr [\]] [\)] | cat
The output is:
()
(]
So far, it can translate other character, except for ']'. Do you know why?
This is a shell exercise, which asks to translate all '{}' and '[]' in a .c file into '()'. I just want to simply it here.
tr
just takes character sets and ranges - you don't need to surround them with []
like regular expression ranges. You can do all your translations with a single command:
echo 'hello [ world }' | tr '[]{}' '()()'
Here's one example that works fine:
echo '{}' |tr '\{\}' '\[\]'
OUTPUT: []
Here's another example:
echo '}' | tr \} \]
OUTPUT: ]
精彩评论