简体   繁体   English

音译行为(shell 中的 tr 程序)

[英]Transliteration behavior (tr program in shell)

I understand the basic behavior of tr but am confused when sets and sequences become involved.我了解 tr 的基本行为,但在涉及集合和序列时感到困惑。 Some examples:一些例子:

$ tr 'a' '[0]'
aaaaaa
[[[[[[
$ tr 'a-z' '[0-9]'
abcdef
[01234
$ tr 'a-z' '[*]'
abcde
[*]]]
$ tr 'a-z' '[\n]'
abcdef
[
]]]]
$ tr 'a-z' '[\n*]'
abcdef






$ tr 'a-z' '\n'
abcdef





It seems to me that in some cases (like with the [\n*] ) it correctly interprets the set, but in other cases, it seems to stall at the closing bracket and continuously output that.在我看来,在某些情况下(比如[\n*] )它正确地解释了集合,但在其他情况下,它似乎停在右括号处并连续 output 那个。 I had thought that sets [] mean "any one of the enclosed characters", and that * would not be treated as a special char within the set.我原以为集合[]的意思是“任何一个包含的字符”,并且*不会被视为集合中的特殊字符。 It seems like this holds true in some cases but not others.这似乎在某些情况下适用,但在其他情况下则不然。 What is going on here?这里发生了什么?

Also, it seems to me that tr 'az' '\n' performs the same action as tr 'az' '[\n*] .此外,在我看来tr 'az' '\n'执行与tr 'az' '[\n*]相同的操作。 Is there some nuance I'm missing?我缺少一些细微差别吗?

[ only have special meaning if you are providing the set in the form [CHAR*] , [CHAR*REPEAT] , [:POSIXCLASS:] , or [=CHAR=] . [只有在以[CHAR*][CHAR*REPEAT][:POSIXCLASS:][=CHAR=]形式提供集合时才具有特殊含义。

From your examples, only tr 'az' '[\n*]' is interpreted as set ("in SET2, copies CHAR until length of SET1");从您的示例中,只有tr 'az' '[\n*]'被解释为集合(“在 SET2 中,复制 CHAR 直到 SET1 的长度”); all other examples are interpreted literally.所有其他示例均按字面解释。

So tr 'af' '[*]' is identical to writing tr 'abcdef' '[*]' and will translate a→[, b→*, c→], d→], e→], f→].所以tr 'af' '[*]'等同于写tr 'abcdef' '[*]'并且会翻译 a→[, b→*, c→], d→], e→], f→]。

tr 'af' '[\n*]' however is identcal to writing tr 'abcdef' '\n\n\n\n\n\n' and will replace every character with a newline. tr 'af' '[\n*]'然而与编写tr 'abcdef' '\n\n\n\n\n\n'相同的,并且将用换行符替换每个字符。 For a single character, this is already the default with tr , so you could have simply written tr 'af' '\n' as you have found out with your last example.对于单个字符,这已经是tr的默认值,因此您可以简单地编写tr 'af' '\n'就像您在上一个示例中发现的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM