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
16
votes
8 answers

What is the `tr` command in Windows?

What is the Windows equivalent of the tr command in Linux? For example, tr can replace all colons with a newline character. Can this be done in Windows? $ echo $PATH | tr ':' '\n' /usr/local/bin /usr/bin
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
13
votes
8 answers

Split sentences into separate lines

I'm trying to split sentences in a file into separate lines using a shell script. Now I would like to split the strings by !, ? or . . The output should be like this : The file that I want to read from my_text.txt and contains you want to learn…
Abdallah_98
  • 1,331
  • 7
  • 17
13
votes
3 answers

Unix tr command to convert lower case to upper AND upper to lower case

So I was searching around and using the command tr you can convert from lower case to upper case and vice versa. But is there a way to do this both at once? So: $ tr '[:upper:]' '[:lower:]' or $ tr A-Z a-z Will turn "Hello World ABC" to "hello…
truffle
  • 455
  • 1
  • 9
  • 17
12
votes
2 answers

Perl: tr/// is not doing what I expect whereas s/// is

I want to remove diacritic signs in some strings. tr/// should do the job but fails (see below). I thought I had an encoding/decoding problem, but I noticed s/// works as I expect. Could somebody explain why? Here is an example of results I get: my…
Georg
  • 1,078
  • 2
  • 9
  • 18
11
votes
5 answers

If pattern matched delete newline character in that line

Let say pattern is string "Love" input This is some text Love this or that He is running like a rabbit output This is some text Love this or thatHe is running like a rabbit I've noticed that sed is very unpleasant for deleting newline characters,…
josifoski
  • 1,696
  • 1
  • 14
  • 19
10
votes
1 answer

Trying to delete non-ASCII characters only

I am trying to manipulate a text file and remove non-ASCII characters from the text. I don't want to remove the line. I only want to remove the offending characters. I am trying to get the following expression to work: sed '/[\x80-\xFF]/d'
M_x_r
  • 596
  • 4
  • 11
  • 26
9
votes
4 answers

removing backslash with tr

So Im removing special characters from filenames and replacing with spaces. I have all working apart from files with single backslashes contained therein. Note these files are created in the Finder on OS X…
rndy
  • 107
  • 1
  • 6
8
votes
6 answers

how to display users under a group in a line for each user in Linux bash

Currently when I run a grep command on a visitors group that I had created grep visitors:x:1011: /etc/group This is a sample result of the above command, as the actual result is too…
7
votes
2 answers

Concordance of text

I have been reading the cookbook for Linux to get a hang of it. I am fairly new to it. I cam across a topic called Concordance of text. Now I understand what it is, but I am not able to get a sequence of commands using tr, sort and uniq ( That's…
user723556
7
votes
6 answers

How do I delete newlines ('\n', 0x0A) from non-empty lines using tr(1)?

I have a file named file1 with following content: The answer t o your question A conclusive a nswer isn’t al ways possible. When in doubt, ask pe ople to cite their so urces, or to explain Even if we don’t agre e with you, or tell y ou. I would…
Martin
  • 957
  • 7
  • 25
  • 38
7
votes
2 answers

remove control characters from string using TR

Please try the following commands: echo "(After Sweet Memories) Play Born To Lose Again" | tr '(' '-' | tr ')' '-' | tr ' ' '-' -After-Sweet-Memories--Play-Born-To-Lose-Again (notice the double --) I'm unable to modify the command and pass a…
Ian Arman
  • 93
  • 3
  • 9
7
votes
4 answers

Transliteration script for linux shell

I have multiple .txt files containing text in an alphabet; I want to transliterate the text into an other alphabet; some characters of alphabet1 are 1:1 with those of alphabet2 (i.e. a becomes e), whereas others are 1:2 (i.e. x becomes ch). I would…
user3946687
7
votes
9 answers

How to replace spaces and slash in string in bash?

Giving the string: foo='Hello \ World! \ x we are friends here we are' Supose there are also tab characters mixed with spaces after or before the \ character. I want to replace the spaces, tabs and the slash by only a space. I tried…
mllamazares
  • 7,876
  • 17
  • 61
  • 89
7
votes
2 answers

How to pipe in the result of another UNIX command

Right now I'm using another text file to store the result of one UNIX command and then using that file to run another command, like so: tr -d "[,|.]" < text > temporary.txt tr "[A-Z]" "[a-z]" < temporary.txt > result.txt How do I combine these two…
user1610862
  • 117
  • 1
  • 1
  • 5
6
votes
3 answers

To hook or not to hook - git

Our bespoke IDE outputs XML files with an encoding that makes them look like binary files. Diffs and merges of these files fail. We can create ASCII versions of these files with the tr command. I would like to get to a state where these files are…
Synesso
  • 37,610
  • 35
  • 136
  • 207
1
2
3
44 45