▼すべてを開く。
▲すべてを閉じる。
$ cat composer.txt Bach 1685 1750 Beethoven 1770 1827 Chopin 1810 1849 Hendel 1685 1759 Mozart 1756 1791
$ tr -s '\t' < composer.txt Bach 1685 1750 Beethoven 1770 1827 Chopin 1810 1849 Hendel 1685 1759 Mozart 1756 1791
trコマンドで-sオプション、重複するタブ「'\t'」、リダイレクト「<」でファイル「composer.txt」をtrコマンドに与えると、タブの重複を一つのタブに変換する。 2行目冒頭のタブは1個なのでこれは取り除かれない。あくまでも2個以上のタブを1つにする。
$ tr -d '\r' < dosfile > unixfile
trコマンドで-dオプション、削除する文字列「'\r'」、変換するファイル「dosfile」をリダイレクトでtrコマンドに与えて、コマンド出力を「unixfile」にリダイレクトで出力すると、改行コードの「0x0D」が削除される。これにより、Windows・DOSの改行コードがUNIXの改行コードに変換される。
改行コードを8進数のキャラクタコードで表すと「015」であるので、8進数表記として「\015」を使って、以下のコマンドでも同様に改行コードを変換できる。
$ tr -d '\015' < dosfile > unixfile
$ cat report.txt Weekly Report 2005/06/17 System configuration No change. Update Not Apply. User Add "guest".
$ tr -s '\n' < report.txt Weekly Report 2005/06/17 System configuration No change. Update Not Apply. User Add "guest".
trコマンドで-sオプション、重複する文字「'\n'」、リダイレクト「<」でファイル「file1.txt」をtrコマンドに与えると、改行文字の重複を一つの改行文字に変換する。
$ cat desc.txt The tr command replace each input sequence of a repeatted character with a single occurence of that character.
$ tr -s 't' < desc.txt The tr command replace each input sequence of a repeated character with a single occurence of that character.
trコマンドで-sオプション、重複する文字「't'」、リダイレクト「<」でファイル「desc.txt」をtrコマンドに与えると、2行目の単語「repeatted」の文字「t」の重複を削除する。
$ cat desc.txt The tr command replace each input sequence of a repeatted character with a single occurence of that character.
$ tr -d 't' < desc.txt The r command replace each inpu sequence of a repeaed characer wih a single occurence of ha characer.
trコマンドで-dオプション、削除する文字列「't'」、リダイレクト「<」でファイル「desc.txt」をtrコマンドに与えると、文字「t」を削除する。