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
6
votes
3 answers

Replace pipe character "|" with escaped pip character "\|" in string in bash script

I am trying to replace a pipe character in an String with the escaped character in it: Input: "text|jdbc" Output: "text\|jdbc" I tried different things with tr: echo "text|jdbc" | tr "|" "\\|" ... But none of them worked. Any help would be…
Álvaro Pérez Soria
  • 1,443
  • 1
  • 13
  • 26
6
votes
3 answers

How to remove the literal string "\n" (not newlines) from a variable in bash?

I'm pulling some data from a database and one of the strings I'm getting back is in one line and contains multiple instances of the string \n. These are not newline characters; they are literally the string \n, i.e. backslash+en, or hex 5C 6E. I've…
Void
  • 115
  • 1
  • 10
6
votes
1 answer

Can somebody explain the tr /d modifier in perl?

I found this example in a Perl tutorial, but could not understand the output. The tutorial says: The /d modifier deletes the characters matching SEARCHLIST that do not have a corresponding entry in REPLACEMENTLIST. but I could not figure out how…
leo
  • 357
  • 2
  • 15
6
votes
2 answers

Remove ^@ Characters in a Unix File

I have a question about removing invisible characters which can be only be seen when we try to view the file using "vi" command. We have a file that's being generated by Datastage Application (Source is a DB2 Table -> Target is a .txt File). File…
Manikanta G
  • 61
  • 1
  • 1
  • 2
6
votes
4 answers

Substring substitution in bash

my problem of today is to replace in a string like this --> 6427//6422 6429//6423 6428//6421 every // with a ,. I tried with different commands: finalString=${startingString//[//]/,} doesn't work fileTemp=$(echo -e "$line\n" | tr "//" "," does a…
Mattia Baldari
  • 567
  • 1
  • 5
  • 15
6
votes
1 answer

How to Use Find - exec and Tr to process a large number of files

I'm having a little trouble with find and exec in bash: Suppose I have a bunch of files that I need to replace '\r' characters in. (previous question: Joining Columns on Command Line with Paste or PR Not Working) For each file, I want to read it,…
Max Song
  • 1,607
  • 2
  • 18
  • 26
5
votes
11 answers

Format text with sed or awk

Am trying to format the below actual output to get in the same line for each disks 0. ct1d0 /pci@4,0/pci8086,347c@4/e,487c@0/disk@1 /dev/chassis/SYS/DBP/HDD0/NVME/disk 1. c2t1d0…
user3132525
  • 123
  • 6
5
votes
4 answers

How can I remove the stop words from sentence using shell script?

I'm trying to remove stop words from sentences in file? Stop Word which I mean : [I, a, an, as, at, the, by, in, for, of, on, that] I have these sentences in file my_text.txt : One of the primary goals in the design of the Unix system was…
Abdallah_98
  • 1,331
  • 7
  • 17
5
votes
1 answer

Escape status within a string literal as argument of `String#tr`

There is something mysterious to me about the escape status of a backslash within a single quoted string literal as argument of String#tr. Can you explain the contrast between the three examples below? I particularly do not understand the second…
sawa
  • 165,429
  • 45
  • 277
  • 381
5
votes
2 answers

tr -d to remove an exact string of characters from a string

I am trying to make tr -d remove a string of characters from an existing string, without it removing characters everywhere else. For example, I want tr to remove : OK from the end of every string in foo.txt. Contents of foo.txt: BROKEN BONES:…
leetbacoon
  • 1,111
  • 2
  • 9
  • 32
5
votes
1 answer

Removing punctuation and tabs with sed

I am using the following to remove punctuation, tabs, and convert uppercase text to lowercase in a text file. sed 's/[[:punct:]]//g' $HOME/file.txt | sed $'s/\t//g' | tr '[:upper:]' '[:lower:]' Do I need to use these two separate sed commands to…
I0_ol
  • 1,054
  • 1
  • 14
  • 28
5
votes
1 answer

Behaviour of tr -c -d while deleting bytes with values that are not characters

I am having trouble understanding this paragraph from the 'RATIONALE' section of http://pubs.opengroup.org/onlinepubs/9699919799/utilities/tr.html. The ISO POSIX-2:1993 standard had a -c option that behaved similarly to the -C option, but did not…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
5
votes
3 answers

Qt, use of "&" in tr

I'm going through the Qt5 Application tutorial. In the creation of actions, I keep seeing the usage of "&" like the follwoing: newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this) ... openAct = new QAction(QIcon(":/images/open.png"),…
Asy
  • 313
  • 1
  • 3
  • 11
5
votes
1 answer

Shell Scripting: Using grep and tr in the shebang

I have this script on my system: #! /bin/grep cat caterpillar cat lol morecat When I run it, I get the output as expected, #! /bin/grep cat caterpillar cat morecat But, if I use tr instead, with the script #! /usr/bin/tr 'a'…
tomKPZ
  • 827
  • 1
  • 10
  • 17
5
votes
3 answers

How to use sed to replace the first space with an empty string

I am having trouble loading a space delimited text file into a table. The data in this text file is generated by teragen, and hence, is just dummy data, where there are only 2 columns, and the first column has values of random special character…
user2432819
  • 71
  • 1
  • 2
  • 6
1 2
3
44 45