Linux Command - Tr | Xing's Blog

0%

tr - Translate characters.

Translate, squeeze, and/or delete characters from standard input, writing to standard output.

Syntax
1tr [OPTIONS] SET1 [SET2]
Options
Common Option Description
-c use the complement of SET1
-d delete characters in the first set
-s replace each input sequence of a repeated character that is listed in SET1 with a single occurrence of that character
-t first truncate SET1 to length of SET2
Examples

Replace characters

1tr 'abc' 'DEF' < file1

Here we replace ‘a’ with ‘D’, ‘b’ with ‘E’ and ‘c’ with ‘F’.

Remove Characters

1tr -d 'ac' < file1

Remove character ‘a’ and ‘c’

Compress Characters

1tr -s '[:blank:]' < file1

Blanks will be compressed. e.g. ‘ab cd’ will become ‘ab cd’

Lower case to upper case

1tr '[:lower:]' '[:upper:]' < file1
Reference
  • tr online man page

Từ khóa » Xing Tr