2018-11-12 08:28:58 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
author: Alessandro Luini
|
|
|
|
category-page: advanced
|
|
|
|
category-title: Advanced commands
|
|
|
|
tags: translate
|
|
|
|
title: tr
|
2018-11-18 20:38:56 +00:00
|
|
|
previous-page: pages/cmd/advanced/pipes.html
|
|
|
|
next-page: pages/cmd/advanced/vi.html
|
2018-11-12 08:28:58 +00:00
|
|
|
---
|
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
The <code>tr</code> command in Unix-like operating systems, which name stands for
|
|
|
|
<i>TRanslate</i> or <i>TRansliterate</i>, indicating its operation of replacing or removing
|
|
|
|
specific characters in its input data set.
|
|
|
|
|
|
|
|
<h3>How to use</h3>
|
|
|
|
|
|
|
|
The utility reads a byte stream from its standard input and writes the
|
|
|
|
result to the standard output. As arguments, it takes two sets of
|
|
|
|
characters (generally of the same length), and replaces occurrences of
|
|
|
|
the characters in the first set with the corresponding elements from the
|
|
|
|
second set.<br>
|
|
|
|
|
|
|
|
For example,
|
|
|
|
<pre>
|
|
|
|
tr 'abcd' 'jkmn'
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
would maps all characters a to j, b to k, c to m, and d to n.<br>
|
|
|
|
The character set may be abbreviated by using character ranges. The
|
|
|
|
previous example could be also written as:
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
tr 'a-d' 'jkmn'
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
<h3>Flags</h3>
|
|
|
|
<ul>
|
|
|
|
<li> <b>-s</b>: The s flag causes tr to compress sequences of identical
|
|
|
|
adjacent characters in its output to a single token.
|
|
|
|
<pre>
|
|
|
|
tr -s '\n'
|
|
|
|
</pre>
|
|
|
|
replaces sequences of one or more newline characters with a single
|
|
|
|
newline.
|
|
|
|
</li>
|
|
|
|
<li> <b>-d</b>: The d flag causes tr to delete all tokens of the specified
|
2018-11-12 08:28:58 +00:00
|
|
|
set of characters from its input. In this case, only a single character
|
|
|
|
set argument is used. The following command removes carriage return
|
|
|
|
characters.
|
2018-11-14 08:56:17 +00:00
|
|
|
<pre>
|
|
|
|
tr -d '\r'
|
|
|
|
</pre>
|
|
|
|
</li>
|
|
|
|
</ul>
|