tr

Deletes or replaces a string. The strings before and after the replacement will be 1:1, so the length of the strings must be the same. This is the standard input.

TR(1) BSD General Commands Manual

NAME
     tr -- translate characters

SYNOPSIS
     tr [-Ccsu] string1 string2
     tr [-Ccu] -d string1
     tr [-Ccu] -s string1
     tr [-Ccu] -ds string1 string2

github

  • The file in jupyter notebook format on github is here.

google colaboratory

  • To run it in google colaboratory here tr_nb.ipynb)

Environment

The author’s OS is macOS, and the options are different from those of Linux and Unix commands.

When you actually run the command, you will need to use the prefix ! and %%bash in the first line.

!sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G2022
!bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

Example usage

Create the string abc 123, read it from standard input, and replace abc with efg.

%%bash
echo "abc 123" > temp
cat temp | tr abc efg
efg 123

Use the -d option to remove the string.

%%bash
echo "abc 123" > temp2
cat temp2 | tr -d 123
abc

I think the above two are the main ways to use it. I use them moderately often. I like sed better, so I use it more often.

Typical options

  • d : delete