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)

189 questions
121
votes
7 answers

Convert file contents to lower case

I have temp file with some lower-case and upper-case contents. Input Contents of my temp file: hi Jigar GANDHI jiga I want to convert all upper to lower. Command I tried the following command: sed -e "s/[A-Z]/[a-z]/g" temp but got wrong…
JigarGandhi
  • 4,820
  • 10
  • 27
  • 38
118
votes
6 answers

Replace multiple spaces with one using 'tr' only

I have a file, f1.txt: ID Name 1 a 2 b 3 g 6 f The number of spaces is not fixed. What is the best way to replace all the white spaces with one space using only tr? This is what I have so far: cat f1.txt | tr -d "…
gkmohit
  • 3,219
  • 8
  • 27
  • 30
57
votes
6 answers

Why can't tr read from /dev/urandom on OSX?

A colleague suggested creating a random key via the following command: tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= < /dev/urandom | head -c 32 | xargs It gave me the error: tr: Illegal byte sequence I'm concerned that I do not have /dev/urandom on…
Kirk Woll
  • 1,117
  • 2
  • 9
  • 9
55
votes
6 answers

Replace all newlines to space except the last

How can I replace all newlines with space except the last newline. I can replace all newline to space using tr but how I can do it with some exceptions?
user59930
51
votes
3 answers

Removing all spaces, tabs, newlines, etc from a variable?

This is the error I am getting and it's failing because of a variable whose value is supposed to be 2 (I am getting this using a select * from tabel). I am getting spaces in that variable. + 0 != 2 ./setjobs[19]: 0: not found. How do I…
munish
  • 7,825
  • 24
  • 71
  • 97
42
votes
2 answers

How to make tr aware of non-ascii(unicode) characters?

I'm trying to remove some characters from file(UTF-8). I'm using tr for this purpose: tr -cs '[[:alpha:][:space:]]' ' '
MatthewRock
  • 6,826
  • 6
  • 31
  • 54
42
votes
2 answers

tr complains of “Illegal byte sequence”

I'm brand new to UNIX and I am using Kirk McElhearn's "The Mac OS X Command Line" to teach myself some commands. I am attempting to use tr and grep so that I can search for text strings in a regular MS-Office Word Document. $ tr '\r' '\n' <…
user74886
  • 421
  • 1
  • 4
  • 4
23
votes
8 answers

Turning separate lines into a comma separated list with quoted entries

I have the following data (a list of R packages parsed from a Rmarkdown file), that I want to turn into a list I can pass to R to install: d3heatmap data.table ggplot2 htmltools htmlwidgets metricsgraphics networkD3 plotly reshape2 scales stringr I…
fbt
  • 363
  • 1
  • 2
  • 6
22
votes
6 answers

Generating a random password; why isn't this portable?

I want to generate a random password, and am doing it like so:
Taymon
  • 491
  • 1
  • 4
  • 9
20
votes
5 answers

How do I remove spaces from shell variables?

I have done the following at command line: $ text="name with space" $ echo $text name with space I am trying to use tr -d ' ' to remove the spaces and have a result of: namewithspace I've tried a few things like: text=echo $text | tr -d ' ' No…
user3347022
  • 465
  • 2
  • 6
  • 14
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…
17
votes
3 answers

Can I use `sed` to translate characters like with `tr`?

I would like to replace a set of characters with corresponding characters from another set, something like this: original set: ots "target" set: u.x foobartest → fuubar.ex. Translations/transliterations like this are the specialty of the tr…
n.st
  • 7,918
  • 4
  • 35
  • 53
16
votes
2 answers

Fastest & Most Core way to replace "\r\n" with "\n" in a file?

I suppose tr is a more core method and thus probably a faster way to replace things within a given file. However tr can only replace equal amounts of characters. meaning... 2 characters can only be replaced with 2 characters which means replacing…
user73235
  • 163
  • 1
  • 1
  • 4
13
votes
3 answers

Main difference between tr (translate) to sed and awk

AFAIC both sed and awk are general purpose text processing utilities with whom a user can get quite similar results, in a slightly different syntax: With both, a user could add, replace/translate and delete content in a file. What is the main…
Arcticooling
  • 1
  • 12
  • 44
  • 103
13
votes
1 answer

Why does the tr command not read from file?

I looked up a Unix book, the man and the wikipedia page for tr but could not find a reason why it was designed/implemented in such way that it does not read from file but strictly only from the standard input. For instance, tools such as wc, grep,…
Ketan Maheshwari
  • 9,054
  • 6
  • 40
  • 53
1
2 3
12 13