TR(1B)

πŸ“¦ version 070715, in nixos 26.05, nixpkgs unstable

πŸ’… TR(1B) β€” like, the BSD translate-characters moment ✨

okay bestie, gather round, because this one is a whole vibe. it's the BSD compatibility version of tr β€” translate characters β€” and yes, she lives at /ucb/tr, not just anywhere. the manual page calls herself TR(1B) and comes to you straight out of the BSD System Compatibility corner of the universe πŸ¦‹


🌸 SYNOPSIS 🌸

/ucb/tr [-cCds] [string1 [string2]]

that's it. that's the whole entrance. two little strings, a handful of flags, and a dream πŸ’–


✨ DESCRIPTION ✨

so here's the tea, fr fr: tr copies standard input to standard output, but with substitution or deletion of selected characters along the way. glow-up included, no extra charge πŸ’…

the input characters you find in string1 get mapped into the corresponding characters of string2. and when string2 is shorter than string1? she doesn't panic β€” she gets padded to the length of string1 by duplicating her last character. iconic behaviour, honestly πŸ‘‘

any combination of the options -cds may be used:

  • -c β€” complements the set of characters in string1 with respect to the universe of characters whose byte codes are 01 through 0377 octal, or, with a multibyte character set, whose wide character codes start at 1. (yes, the complement era πŸͺž)
  • -d β€” deletes all input characters in string1. gone. not invited to the group chat πŸ’€
  • -s β€” squeezes all strings of repeated output characters that are in string2 to single characters. one of each, she's not doing duplicates πŸŽ€

the following option has been introduced by POSIX.1-2001 (so she's, like, officially certified ✨):

  • -C β€” complements the set of characters in string1 like -c does, but orders ranges according to the collation sequence. same energy, different ordering energy πŸŒ€

in either string, these character sequences are treated specially β€” memorize these, they're the main characters:

  • \octal β€” the character \ followed by 1, 2 or 3 octal digits stands for the character whose byte code is given by those digits. multibyte characters can be specified as a sequence of octal bytes. (so like, \012 is a whole thing, ok? πŸ“)
  • \char β€” the escape sequences \a (bell), \b (backspace), \f (form feed), \n (newline), \r (carriage return), \t (horizontal tabulator), and \v (vertical tabulator) are supported. a \ followed by any other character (other than an octal digit) stands for that character. basically the backslash is a lil escape artist 🧚
  • a-z β€” means a range of characters from a to z in increasing byte order, or, with a multibyte character set, in increasing wide character order. with the -C option, characters are ordered according to the collation sequence 🌈
  • [:class:] β€” means all characters that belong to character class class in the current LC_CTYPE locale in increasing byte order, or, with a multibyte character set, in increasing wide character order. and here's the cute part: if both [:upper:] and [:lower:] appear at the same position in either string, upper-case characters are mapped to lower-case characters (and vice versa!). a literal swap, we love to see it πŸ”„
  • [=c=] β€” where c is a collating symbol in the current LC_COLLATE locale, means all characters that belong to the same equivalence class as c, i. e. have the same collating weight as c. (same weight = same squad βš–οΈ)
  • [a*n] β€” means n repetitions of the character a, with n as an octal number if it starts with 0 and as a decimal number otherwise. if a is omitted or zero, it is taken to be huge β€” super useful for padding string2 to the length of string1, like, the ultimate filler arc 🫢

πŸ¦‹ ENVIRONMENT VARIABLES πŸ¦‹

  • LANG, LC_ALL β€” see locale(7). the classics πŸ’œ
  • LC_COLLATE β€” affects the composition of equivalence classes. (well… see the NOTES, bestie, there's a plot twist 😭)
  • LC_CTYPE β€” determines the mapping of bytes to characters in translation strings and input files, and the availability and composition of character classes. she decides who's who and what's what πŸ‘‘

πŸ”₯ EXAMPLES πŸ”₯

the following examples create a list of all the words in file1, one per line, in file2 β€” where a word is taken to be a maximal string of alphabetics. the second string is quoted to protect \ from the Shell. 012 is the ASCII code for newline. periodt.

/ucb/tr -cs A-Za-z Β΄\012Β΄ <file1 >file2

like, look at her go πŸ˜­πŸ’–


πŸ‘‘ SEE ALSO πŸ‘‘

ed(1), ascii(7), locale(7) β€” go read those too, iykyk ✨


πŸ’€ NOTES πŸ’€

okay important caveats, do not skip these: - tr does not handle ASCII NUL in string1 or string2; it always deletes NUL from input. no cap, NUL is just not welcome here πŸ™ˆ - the LC_COLLATE variable is not respected; equivalence classes consist of exactly one character, and the -c and -C options produce identical results. yeah… the collation thing is lowkey just for show in this version 🫠

and that's the whole page, bestie! this lil beauty comes to you from the Heirloom Toolchest, dated 8/6/05 β€” go forth and translate those characters, you absolute main character πŸŒŸπŸŽ€πŸ’…

πŸ“¦ where this page comes from