Questions tagged [tr]

For questions pertaining to tr, a utility that translates or deletes characters. Use this tag for questions about the tr utility itself, or questions about issues arising from its use.

tr (abbreviated from translate or transliterate) is a command in Unix-like operating systems. When executed, the program reads from the standard input and writes to the standard output. It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set.

External reference

tr specification (The Open Group Base Specifications Issue 7, 2018 edition)

192 questions
18
votes
3 answers

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

I want to use tr to do some rot13 transformation. I can beautifully understand this command: tr A-Za-z N-ZA-Mn-za-m <<< "URYC ZR CYRNFR" which output is HELP ME PLEASE, but I can't figure out how this other command can produce the same rot13…
14
votes
2 answers

How does tr '[a-z]' '[n-za-m]' work?

I found an obfuscated e-mail address that can be decrypted using this command: echo "cbyrzba@cbyrzba.bet" | tr '[a-z]' '[n-za-m]' How does that output a valid e-mail address? What is that command doing?
LanceBaynes
  • 40,135
  • 97
  • 255
  • 351
13
votes
2 answers

Can I disable buffering for tr

tr seems to buffer its input so that this command LongRunningCommand|tr \\n , will only start producing output after a few kilobytes of input from LongRunningCommand have accumulated. Is there a way to force tr to stop this buffering or any other…
ndemou
  • 2,809
6
votes
1 answer

Delete only first occurrence of character using tr

I want to delete the occurrence of a character in a string only for the first occurrence. Example: echo "B123_BACK" | tr -d 'B' This results in output: 123_ACK How can I delete only the first occurrence of charcater 'B' so that the output looks…
g4ur4v
  • 1,764
  • 8
  • 29
  • 34
5
votes
2 answers

what is the benefit of --squeeze-repeats in tr command

What is the benefit of -s or --squeeze-repeats when we use tr? What practical usage?
Majid Azimi
  • 3,098
3
votes
1 answer

understanding the difference between "-C" and "-c" in tr(1) utility

According to tr(1) manual -C means: Complement the set of characters in string1, that is ``-C ab'' includes every character except for `a' and `b'. ..and -c means: Same as -C but complement the set of values in string1. Now if I use -c in the…
Martin
  • 7,516
2
votes
1 answer

tr replace not with space but delete char

I have a string animal: dog and I would like to transform to have just animal dog (one space between). e.g: echo 'animal: dog' | tr ':' ' ' animal dog There's 2 spaces above. I tried making the replace an empty string but: echo 'animal: dog' | tr…
Doug Fir
  • 133
  • 1
  • 5
2
votes
1 answer

I cannot understand what -c parameter does in tr command in Ubuntu GNU/Linux even though I read the manual

I try to understand what -c option does in tr command according to man tr command it says: -c, -C, --complement use the complement of SET1 But I cannot understad what the doc mean by "complement" so I did the following example to…
1
vote
1 answer

Why does tr -sc 'A-Za-z' '[\12*]' includes empty line?

I'm working through Unix For Poets, and trying make a file containing all words/tokens in the Bible. However, when using tr, as suggested, this includes the empty string. See example below: > tr -sc 'A-Za-z' '[\12*]' < bible.txt > bible.words > sed…
Ola R
  • 13
1
vote
1 answer

Escaping the dot character in tr -dc

I'm trying to rename files using tr. The following command nearly works: for file in ./*; do mv -v "$file" $(echo "$file" | tr ' []' '-' | tr -dc 'A-Za-z0-9_-' | tr '[:upper:]' '[:lower:]'); done However, the command also strips dot characters. So,…
rkhff
  • 795
1
vote
2 answers

how to substitute minus sign with tr

I'm trying to replace several symbols with spaces, and I thought tr was the right command. So I tried cat myfile | tr '_[]()-/' ' ' But I get the error range-endpoints of ']-/' are in reverse collating sequence order presumably because I can't…
0
votes
2 answers

Using tr to reformat a txt file so that each lines contains 1 word

I want to modify a file to remove all punctuation, numbers and uppercases and also change the file so that there is only 1 word per line exemple: Hello, how are you! hello how are you With some help I came up with this: tr -d '[:punct:]' <…
gabriel
  • 11
0
votes
2 answers

tr with line number

This is example of tr output user@linux:~$ tr $ '\n' <<< 'abc$def$ghi' abc def ghi user@linux:~$ Would it be possible to add line number to each line? E.g. user@linux:~$ tr $ '\n' <<< 'abc$def$ghi' 1. abc 2. def 3. ghi user@linux:~$
user264359