使用tr -t命令【理解题】

erc*_*rch 1 utilities tr

使用tr -t命令时,string1应该截断到 的长度string2,对吗?

tr -t abcdefghijklmn 123          # abc... = string1, 123 = string2
the cellar is the safest place    # actual input
the 3ell1r is the s1fest pl13e    # actual output
Run Code Online (Sandbox Code Playgroud)

“截断”是“缩短”的另一种说法,对吗?tr根据模式翻译,完全忽略-t选项。如果我自动完成--truncate-set1[以确保我使用正确的选项] 会产生相同的输出。

问题:我在这里做错了什么?

我在基于 Debian 的发行版上的 BASH 中工作。

更新

请注意,这是我在下面发表的评论的副本

我认为的tr -t意思是:将 string1 缩短为 string2 的长度。我看到它a被翻译成1,那b将被翻译成2,那c被翻译成3。这与缩短无关。“截断”的意思似乎与我想象的不同。[我不是母语者]

gol*_*cks 5

使用 tr -t 命令时,应该将 string1 截断为 string2 的长度,对吗?

不就是这么回事吗?

abcdefghijklmn
123
Run Code Online (Sandbox Code Playgroud)

注意哪些字母被交换了,哪些没有被交换:

the 3ell1r is the s1fest pl13e
Run Code Online (Sandbox Code Playgroud)

'a' 和 'c',但不包括 e、f、i 或 l,它们在原始(未截断的)集合 1 中。

没有-t,你会得到:

t33 33331r 3s t33 s133st p3133
Run Code Online (Sandbox Code Playgroud)

这是因为(从man tr),“SET2通过根据需要重复其最后一个字符而扩展到 SET1长度。” 因此,如果没有-t截断集 1,您所拥有的与

tr abcdefhijklmn 1233333333333
Run Code Online (Sandbox Code Playgroud)

让我们考虑另一个例子,但使用相同的“地窖是最安全的地方”作为输入。

> input="the cellar is the safest place"
> echo $input | tr is X
the cellar XX the XafeXt place
Run Code Online (Sandbox Code Playgroud)

这是因为第二组会自动扩展以覆盖第一组的所有内容。 -t本质上与此相反;它截断第一组而不是扩展第二组:

> echo $input | tr -t is X
the cellar Xs the safest place
Run Code Online (Sandbox Code Playgroud)

这与以下内容相同:

> echo $input | tr i X
the cellar Xs the safest place
Run Code Online (Sandbox Code Playgroud)

由于“s”从第一组中被截断。如果两组长度相同,则使用-t不会有任何区别:

> echo $input | tr is XY
the cellar XY the YafeYt place
> echo $input | tr -t is XY
the cellar XY the YafeYt place
Run Code Online (Sandbox Code Playgroud)