Skip to main content

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.

Filter by
Sorted by
Tagged with
122 votes
7 answers
218k views

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: ...
JigarGandhi's user avatar
  • 5,030
121 votes
6 answers
291k views

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? ...
gkmohit's user avatar
  • 3,359
58 votes
6 answers
39k views

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 ...
Kirk Woll's user avatar
  • 1,147
55 votes
6 answers
71k views

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?
user avatar
54 votes
4 answers
140k views

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 != ...
munish's user avatar
  • 8,097
45 votes
2 answers
64k views

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 ...
user74886's user avatar
  • 451
43 votes
2 answers
16k views

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:]]' ' ' <testdata.dat File contains some foreign characters (like "Латвийская" or "...
MatthewRock's user avatar
  • 7,056
24 votes
8 answers
53k views

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 ...
fbt's user avatar
  • 373
21 votes
5 answers
137k views

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 ...
user3347022's user avatar
21 votes
6 answers
5k views

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

I want to generate a random password, and am doing it like so: </dev/urandom tr -dc [:print:] | head -c 64 On my laptop, which runs Ubuntu, this produces only printable characters, as intended. ...
Taymon's user avatar
  • 491
18 votes
3 answers
4k views

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 ...
Frederico Oliveira's user avatar
17 votes
3 answers
8k views

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/...
n.st's user avatar
  • 8,198
17 votes
3 answers
18k views

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 ...
Arcticooling's user avatar
16 votes
2 answers
70k views

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 ...
user73235's user avatar
  • 163
13 votes
2 answers
42k views

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

I found an obfuscated e-mail address that can be decrypted using this command: echo "[email protected]" | tr '[a-z]' '[n-za-m]' How does that output a valid e-mail address? What is that command ...
LanceBaynes's user avatar
  • 40.7k
13 votes
2 answers
4k views

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 ...
ndemou's user avatar
  • 2,879
13 votes
1 answer
5k views

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 ...
Ketan's user avatar
  • 9,306
12 votes
4 answers
40k views

Can tr work with regex?

I have a file like this: AAAA BBBB CCCC DDDD etc Words in separate lines. I want to add them quotes using tr. So I tried the following: 1) To add a quote in the start of each word i....
Jim's user avatar
  • 10.2k
12 votes
4 answers
13k views

how to use tr to replace multiple sets?

I want to use tr to replace digits 0-5 with A and digits 6-9 with B. I tried using sed to do this: sed 's/[0-4]/</; s/[6-9]/>/' It works only once though so for example if I write 123 it will ...
piman's user avatar
  • 129
12 votes
2 answers
8k views

Is there any way to use the tr command in place?

I want to remove all non-english alphanumerals from a file. tr -sc '[:alnum:][:punct:]' ' ' <file gawk and sed have the -i flag but I cannot find anything similar in the tr man pages. How can I ...
user avatar
11 votes
2 answers
3k views

Strange behaviour of tr using ranges

I have one particular server that is exhibiting strange behaviour when using tr. Here is an example from a working server: -bash-3.2$ echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-z] ...
Chris's user avatar
  • 121
11 votes
3 answers
19k views

Can tr commands be chained to avoid multiple tr processes in a pipeline?

I have a bunch of txt files, I'd like to output them lower-cased, only alphabetic and one word-per line, I can do it with several tr commands in a pipeline like this: tr -d '[:punct:]' <...
tlehman's user avatar
  • 601
11 votes
2 answers
3k views

What's the difference between `-C` and `-c` in `tr` command?

Today I learnt a bit about tr command. But I was stuck at understanding the difference between -c and -C. On the manual, it said: -C Complement the set of characters in string1, that is ``-C ...
ZengJuchen's user avatar
  • 7,697
10 votes
3 answers
6k views

How to convert UTF-8 txt files to all uppercase in bash?

I have some UTF-8 .txt files which I would like to convert to all uppercase. If it was just ASCII, I could use: tr [:lower:] [:upper:] But since I'm working with diacritics and stuff, it doesn't ...
VPeric's user avatar
  • 627
10 votes
1 answer
2k views

tr a A < /etc/hosts | sort -r |pr -d > /etc/hosts

In this line: tr a A < /etc/hosts | sort -r |pr -d > /etc/hosts what would be the expected outcome? I know lowercase 'a' would change to uppercase 'A' but what's next? Would the original file ...
tdharrison's user avatar
10 votes
3 answers
5k views

tr analog for unicode characters?

I need internationalized utility that does the same thing as tr: gets character from stream and substitutes it with a corresponding character. Not a particular case solution like lower-to-upper, but a ...
fedoraman's user avatar
  • 123
10 votes
2 answers
8k views

Removing non printable characters from expect logs?

How can I either strip the following character sequences from my files? ^[[64;8H, ^[[?25h, ^[[1;64r etc or avoid having expect add them in the first place? Background: I'm using a collection of ...
Tim Brigham's user avatar
  • 1,027
9 votes
2 answers
3k views

Explanation needed on how I can repeat a character in POSIX shell

The following answer on Stack Overflow, How can I repeat a character in bash? imposes one plausible way of POSIX-ly repeating a single character, as follows. In this example let's use the equal sign ...
Vlastimil Burián's user avatar
8 votes
2 answers
4k views

Why doesn't the command tr "\'\\\"\?\!" "01234" work?

In terminal, if I define some variable char as follows: export char=\'\\\"\?\! In effect, char is the string '\"?! And then I use the tr command to replace '\"?! with numbers 01234 tr "\'\\\"\?\!"...
Robb U's user avatar
  • 81
7 votes
9 answers
12k views

Replace any number of tabs and spaces with single new line in Linux?

Suppose I have a (potentially very large) text file that contains a word list with whitespace interjected.  For example, it might look like this: Cat Dog Soup ...
user2925489's user avatar
7 votes
1 answer
864 views

Is it possible to replace duplicates of a character with one character using tr

I'm trying to take all occurrences of # and if they are consecutive replace them with just one #. e.g. ab ### cde fghi## jklm would become: ab # cde, fghi# jklm My initial goal was to replace ...
user652437's user avatar
7 votes
3 answers
4k views

How do I use tr command to replace: full stops, exclamations and question marks followed by a space, with newlines?

I have text that goes like this: I am happy. I am here. How are you, Meg? I want this to be: I am happy. I am here. How are you, Meg? For full stops I tried tr -s '. ' '\n' <file.txt >out....
user avatar
7 votes
1 answer
20k views

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 ...
g4ur4v's user avatar
  • 1,804
7 votes
2 answers
14k views

Redirecting tr stdout to a file [duplicate]

Possible Duplicate: How can I make iconv replace the input file with the converted output? Can I read and write to the same file in Linux without overwriting it? $ cat test.txt This is a test ...
wrongusername's user avatar
7 votes
2 answers
863 views

Escape characters in sed transliterate

I want to use sed's transliterate (y///) to replace one set of characters by another. I would expect this to work just as well as using the tr program. $ echo '[]{}abc' | tr '[ab}' 'gefh' g]{hefc ...
Jason Hemann's user avatar
7 votes
6 answers
6k views

How to change any text to Proper Case and Sentence case using tr?

According to https://caseconverter.com/ “Upper Case” WHICH CONVERTS ALL THE LETTER INTO CAPITALS LIKE THIS. “Lower Case” which converts all the letters into small letters like this. “...
user avatar
7 votes
4 answers
8k views

Is the command tr capable of translating characters into longer strings?

tr is a great tool that can be used to translate set of characters into another set of characters. Commands such as sed , awk, perl can be used to translate set of characters into another set of ...
user avatar
6 votes
1 answer
317 views

Heirloom Toolchest tr: error(s) trying to delete the complement of a set containing a multibyte character?

I'm trying to use the tr command from the Heirloom Toolchest to overcome a current limitation of the coreutils implementation, so as to be able to "pump" (with the -dc options) multibyte characters ...
user avatar
6 votes
1 answer
3k views

How do I turn accented lowercase letters to uppercase? - Using the 'tr' command [duplicate]

I'm tryed with: $ echo 'Ciência' | tr 'a-z' 'A-Z' CIêNCIA But the 'ê' character does not change, obviously because it is accentuated. Then I tried with POSIX classes: echo "Ciência" | tr '[:lower:]'...
Patterson's user avatar
  • 765
5 votes
5 answers
1k views

How can I transliterate some characters 1-to-1, leave some unchanged, and replace others with the same target character?

I have a file called file.txt containing: MAL TIRRUEZF CR MAL RKZYIOL EX MAL OIY UAE RICF "MAL ACWALRM DYEUPLFWL CR ME DYEU MAIM UL IZL RKZZEKYFLF GH OHRMLZH" I'd like the characters ...
user avatar
5 votes
3 answers
1k views

how to emulate "replace a b b a" with a sed?

I am trying to emulate a replace command which swaps two characters, with a sed script, but the obvious choice: sed -e 's/a/b/g' -e 's/b/a/g' does no work. Can you point me how to do it correctly?
Mitten's user avatar
  • 393
5 votes
1 answer
336 views

Why does 'tr' with '-c' option and set 2 extension add an unwarranted character to the end?

I wanted to use tr to substitute "illegal" characters in a string with a replacement character, where "illegal" characters are all outside of a set of "allowed" ...
AdminBee's user avatar
  • 22.9k
5 votes
3 answers
404 views

Processing a continuous single line of data with stream processing in bash pipeline?

I am debugging an embedded server that outputs a continuous single line of text to a specified network port. There is no newline anywhere in the stream but it is text data and I would like to format ...
stmfunk's user avatar
  • 193
5 votes
1 answer
240 views

Debian wheezy updates broke tr command [closed]

After I updated the following packages yesterday in debian wheezy, tr command stop working. Start-Date: 2016-02-16 23:10:06 Commandline: apt-get dist-upgrade Upgrade: libkrb5-3:amd64 (1.10.1+dfsg-5+...
ncomputers's user avatar
  • 1,554
5 votes
2 answers
4k views

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's user avatar
  • 3,118
4 votes
3 answers
21k views

Pulling IP address from ping command with sed? [duplicate]

I have crafted a Bash tool that runs on a server. This tool will block certain IP addresses for a certain time range (i.e. from 5 A.M. to 3:30 P.M.). As of currently, the tool works fine, but I have ...
Yokai's user avatar
  • 223
4 votes
4 answers
2k views

How do I delete all lines with ".png" in a file?

I've a list of urls of which some point to images: http://s.thebrighttag.com/iframe?c=A5lqOqP http://s.wordpress.com/imgpress?resize=136,136&amp;url=http%3A%2F%2Fdata.whicdn.com%2Fimages%...
simpatico's user avatar
  • 631
4 votes
6 answers
7k views

Linux tr to convert vertical text to horizontal

I've been reading about https://stackoverflow.com/questions/39791042/convert-vertical-text-into-horizontal-in-shell and wondering if tr alone can be used to convert vertical text to horizontal. user@...
user avatar
4 votes
4 answers
6k views

How does "-dc" work in "cat /dev/urandom | tr -dc "01"?

So far I know, the /dev/urandom file is one of the special files, it's purpose is to generate random characters. When I execute cat /dev/urandom a stream of strange characters, some even Chinese ...
Abdul Al Hazred's user avatar
4 votes
6 answers
203 views

Make longer strings from substrings keeping new line between each string

I have a file that contains strings as: GTACGACGGAGTGTTATAAGATGGGAAATCGGATACCAGATGAAATTGTGGATCGGTGCAAAA GTCGGCAGATATCGTTGAAGTCATAGGTGATTATGTTCAATTAAAGAAGCAAGGCCGAAACTAC ...
Faiz Lotfy's user avatar