using tr -t command [comprehension question]
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
Related videos on Youtube
Comments
-
erch over 1 year
When using
tr -t
command,string1
should be truncated to the length ofstring2
, 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 thata
is translated to1
, thatb
would be translated to2
and thatc
is translated to3
. This has nothing to do with shortening. 'To truncate' seems to mean something different than I thought. [I'm no native speaker]-
Admin about 11 yearsWhat other output are you expecting instead?
tr -t abcdefghijklmn 123
is equivalent oftr abc 123
. -
Admin about 11 yearsWhat are you trying to do? It is hard to understand your question.
-
Admin about 11 yearsI think you're confusing "
string1
" withSTDIN
.string1
(an argument; calledSET1
in GNU man page) is truncated, input is not. -
Admin about 11 years@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].
-
Admin about 11 years@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. -
Admin about 11 years@goldilocks may I shyly ask where this would make sense? An example maybe? I found quite some tutorials on
tr
, but hardly anyone seems to use-t
-
Admin about 11 years@cellar.dweller : Alright, I added that to the end of the answer :)
-
-
erch about 11 years[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 thata
is translated to1
, thatb
would be translated to2
and thatc
is translated to3
. This has nothing to do with shortening. 'To truncate' seems to mean something different than I thought. -
goldilocks about 11 years@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 usedtr abc 123
. Everything past c is no longer included in the translation. I added a paragraph better explaining the difference between that andtr abcdefhijklmn 123
.