Questions tagged [tr]

tr is a *nix utility for character-level alterations to a stream. tr/// is a Perl operator named after this utility. For the tag used to build HTML tables, please use [html-table]. Use this tag only if your question relates to programming using tr. Questions relating to using or troubleshooting tr command-line options itself are off-topic.

tr (for "translate") is a standard utility in Unix, GNU/Linux, and other systems. It reads from standard input and writes to standard output, making specified character-level alterations.

One alteration it can make is to substitute individual characters, or groups of characters, with specified replacements; for example, tr a-z A-Z "translates" every occurrence of a lowercase ASCII letter to its uppercase counterpart.

Another alteration is to remove characters; for example, tr -d % "deletes" every instance of a percent sign, while tr -s % "squeezes" sequences of repeated percent signs (so that %% or %%% or %%%% or whatnot will become simply %). tr can also complement characters; for example, tr -cd '[:alnum:]' removes all non-alphanumeric characters.

tr/// is a Perl operator named for this utility. (It can also be written y///, after the same operator in sed.) It substitutes individual characters; for example, tr/a-z/A-Z/ translates every occurrence of a lowercase ASCII letter to its uppercase counterpart.

Beginners often try to use tr for replacements which are not character-level alterations. If you don't want to replace every instance of an individual character with something else, this is the wrong tool; perhaps look at sed, or the s/regex/string/ facility in Perl for regular expression (text pattern) replacement.

Links

675 questions
258
votes
12 answers

How to concatenate multiple lines of output to one line?

If I run the command cat file | grep pattern, I get many lines of output. How do you concatenate all lines into one line, effectively replacing each "\n" with "\" " (end with " followed by space)? cat file | grep pattern | xargs sed s/\n/ /g isn't…
T. Webster
  • 9,605
  • 6
  • 67
  • 94
92
votes
1 answer

Using tr to replace newline with space

Have output from sed: http://sitename.com/galleries/83450 72-profile Those two strings should be merged into one and separated with space like: http://sitename.com/galleries/83450 72-profile Two strings are pipelined to tr in order to replace…
y.bregey
  • 1,469
  • 1
  • 12
  • 21
67
votes
5 answers

Replace slash in Bash

Let's suppose I have this variable: DATE="04\Jun\2014:15:54:26" Therein I need to replace \ with \/ in order to get the string: "04\/Jun\/2014:15:54:26" I tried tr as follows: echo "04\Jun\2014:15:54:26" | tr '\' '\\/' But this results in:…
user109447
  • 1,069
  • 1
  • 10
  • 20
66
votes
3 answers

tr command - how to replace the string "\n" with an actual newline (\n)

I would like to use the tr command to replace all occurrences of the string "\n" with a new line (\n). I tried tr '\\n' '\n' but this just seems to match any '\' and any 'n'
Alastair
  • 1,710
  • 2
  • 17
  • 33
55
votes
6 answers

Character Translation using Python (like the tr command)

Is there a way to do character translation / transliteration (kind of like the tr command) using Python? Some examples in Perl would be: my $string = "some fields"; $string =~ tr/dies/eaid/; print $string; # domi failed $string = 'the cat sat on…
hhafez
  • 38,949
  • 39
  • 113
  • 143
52
votes
5 answers

How to replace one character with two characters using tr

Can tr replace one character with two characters? I am trying to replace "~" with "~\n" but the output does not produce the newline. $ echo "asdlksad ~ adlkajsd ~ 12345" | tr "~" "~\n" asdlksad ~ adlkajsd ~ 12345
Paolo
  • 1,557
  • 3
  • 18
  • 28
49
votes
7 answers

Removing a newline character at the end of a file

To remove all newlines you could, say: tr -d '\n' < days.txt cat days.txt | tr -d '\n' but how would you use tr to remove just the newline at the end/bottom of a text file? I'm not sure to specify just the last one.
user1257724
47
votes
19 answers

Removing trailing / starting newlines with sed, awk, tr, and friends

I would like to remove all of the empty lines from a file, but only when they are at the end/start of a file (that is, if there are no non-empty lines before them, at the start; and if there are no non-empty lines after them, at the end.) Is this…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
42
votes
1 answer

What is the most efficient case-insensitive grep usage?

My objective is to match email addresses that belong to the Yahoo! family of domains. In *nix systems (I will be using Ubuntu), what are the benefits and drawbacks to any one of these methods for matching the pattern? And if there is another, more…
bebingando
  • 1,022
  • 1
  • 8
  • 12
33
votes
5 answers

Delete a specific string with tr

Is it possible to delete a specific string with tr command in a UNIX-Shell? For example: If I type: tr -d "1." and the input is 1.1231, it would show 23 as an output, but I want it to show 1231 (notice only the first 1 has gone). How would I do…
intelinside
  • 415
  • 1
  • 6
  • 6
17
votes
1 answer

Replace \n(new line) with space in bash

I am reading some sql queries into a variable from db and it contains new line character (\n). I want to replace \n (new line) with space. I tried solutions provided on internet but was unsuccessful to achieve what I want. Here is what tried…
Prometheus
  • 549
  • 1
  • 7
  • 18
17
votes
3 answers

Removing all special characters from a string in Bash

I have a lot of text in lowercase, only problem is, that there is a lot of special characters, which I want to remove it all with numbers too. Next command it's not strong enough: tr -cd '[alpha]\n ' In case of éćščž and some others it returns "?"…
Marta Koprivnik
  • 385
  • 2
  • 3
  • 10
17
votes
2 answers

tr [:upper:] [:lower:] with Cyrillic text

I'm trying to extract a word list from a Russian short story. #!/bin/sh export LC_ALL=ru_RU.utf8 sed -re 's/\s+/\n/g' | \ sed 's/[\.!,—()«»;:?]//g' | \ tr '[:upper:]' '[:lower:]' | \ sort | uniq However the tr step is not lowercasing the Cyrillic…
slim
  • 40,215
  • 13
  • 94
  • 127
16
votes
4 answers

unix tr find and replace

This is the command I'm using on a standard web page I wget from a web site. tr '<' '\n<' < index.html however it giving me newlines, but not adding the left broket in again. e.g. echo "" | tr '<' '\n<' returns (blank line which…
Kamran224
  • 1,584
  • 7
  • 20
  • 33
16
votes
3 answers

is there a way to use tr/// (or equivalent) in java?

I would like to know if there is an equivalent to tr/// (as used in Perl) in Java. For example, if I wanted to replace all "s"s with "p"s in "mississippi" and vice versa, I could, in Perl, write #shebang and pragmas snipped... my $str =…
Limao Luo
  • 161
  • 1
  • 4
1
2 3
44 45