1

When using tr -t command, string1 should be truncated to the length of string2, right?

tr -t abcdefghijklmn 123          # abc... = string1, 123 = string2
the cellar is the safest place    # actual input
the 3ell1r is the s1fest pl13e    # actual output

'to truncate' is another word for 'to shorten', right?tr translates, according to the pattern, completely ignoring the -t option. If I autocomplete to --truncate-set1 [to assure that I use the correct option] produces the same output.

Question: what am I doing wrong here?

I work in BASH, on a Debian based Distro.

UPDATE

Please note that this is a copy of a comment I made below

I thought tr -t means: shorten string1 to the length of string2. I see that a is translated to 1, that b would be translated to 2 and that c is translated to 3. This has nothing to do with shortening. 'To truncate' seems to mean something different than I thought. [I'm no native speaker]

  • What other output are you expecting instead? tr -t abcdefghijklmn 123 is equivalent of tr abc 123. – manatwork Apr 26 '13 at 14:54
  • What are you trying to do? It is hard to understand your question. – Rany Albeg Wein Apr 26 '13 at 15:17
  • I think you're confusing "string1" with STDIN. string1 (an argument; called SET1 in GNU man page) is truncated, input is not. – depquid Apr 26 '13 at 15:30
  • @RanyAlbegWein: I seem to completely have misunderstood the meaning of 'to truncate'. I've commented on this under the answer below [and still don't get it]. – erch Apr 26 '13 at 19:34
  • 1
    @cellar.dweller : It might be better if you think of the the two args to tr as "sets" or "patterns" of characters and not strings. Those corresponding sets/related patterns are used in the translation of the input. So the -t in your example means that the first set is now only 3 characters, and that's applied in the translation, which is why the exact same example without -t does, in fact, produce different output. – goldilocks Apr 26 '13 at 20:24
5

When using tr -t command, string1 should be truncated to the length of string2, right?

Isn't that what happened?

abcdefghijklmn
123

Notice which letters are and are not swapped:

the 3ell1r is the s1fest pl13e

'a' and 'c', but not e, f, i, or l, which were in the original (non-truncated) set 1.

Without the -t, you get:

t33 33331r 3s t33 s133st p3133

This is because (from man tr), "SET2 is extended to length of SET1 by repeating its last character as necessary." So without the -t to truncate set 1, what you have is the same as

tr abcdefhijklmn 1233333333333

Let's consider another example, but using the same "the cellar is the safest place" as input.

> input="the cellar is the safest place"
> echo $input | tr is X
the cellar XX the XafeXt place

This is because the 2nd set is automatically extended to cover all of the first set. -t essentially does the opposite of that; it truncates the first set rather than extending the 2nd one:

> echo $input | tr -t is X
the cellar Xs the safest place

Which is the same as:

> echo $input | tr i X
the cellar Xs the safest place

Since the 's' was truncated from the first set. If the two sets were the same length, then using -t won't make any difference:

> echo $input | tr is XY
the cellar XY the YafeYt place
> echo $input | tr -t is XY
the cellar XY the YafeYt place
  • [please note that this comment is a copy of the update that I made on the original post]. I thought tr -t means: shorten string1 to the length of string2. I see that a is translated to 1, that b would be translated to 2 and that c is translated to 3. This has nothing to do with shortening. 'To truncate' seems to mean something different than I thought. – erch Apr 26 '13 at 20:12
  • @cellar.dweller Truncate means to shorten, yes, but tr isn't short for truncate, it's short for translate. The -t switch, however, is for truncating set 1 to the length of set 2 (and think: it would be meaningless the other way around). So, what I was trying to point out is that set 1 has been shortened: you get the exact same output as if you used tr abc 123. Everything past c is no longer included in the translation. I added a paragraph better explaining the difference between that and tr abcdefhijklmn 123. – goldilocks Apr 26 '13 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.