theshell.ch/site/pages/cmd/advanced/tr.html
bevilj 513e934b67 code review
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@219 a672b425-5310-4d7a-af5c-997e18724b81
2018-11-15 20:09:21 +00:00

54 lines
1.5 KiB
HTML

---
layout: page
author: Alessandro Luini
category-page: advanced
category-title: Advanced commands
tags: translate
title: tr
---
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
set of characters from its input. In this case, only a single character
set argument is used. The following command removes carriage return
characters.
<pre>
tr -d '\r'
</pre>
</li>
</ul>