首页
首页 » 文档编辑 » tr(translate character)

tr(translate character)

tr(translate character)
功能说明:转换字符。
语  法:tr [-cdst][--help][--version][第一字符集][第二字符集]
补充说明:tr指令从标准输入设备读取数据,经过字符串转译后,输出到标准输出设备。
参  数:
  -c或--complerment   取代所有不属于第一字符集的字符。
  -d或--delete   删除所有属于第一字符集的字符。
  -s或--squeeze-repeats   把连续重复的字符以单独一个字符表示。
  -t或--truncate-set1   先删除第一字符集较第二字符集多出的字符。
  --help   在线帮助。
  --version   显示版本信息。

tr

tr [options] [string1 [string2] ]

Translate characters. Copy standard input to standard output, substituting characters from string1 to string2, or deleting characters in string1.

Options

-c, --complement

Complement characters in string1 with respect to ASCII 001-377.

-d, --delete

Delete characters in string1 from output.

-s, --squeeze-repeats

Squeeze out repeated output characters in string2.

-t, --truncate-set1

Truncate string1 to the length of string2 before translating.

--help

Print help message and then exit.

--version

Print the version number and then exit.

Special characters

Include brackets ([ ] ) where shown.

a

Ctrl-G (bell)



Ctrl-H (backspace)

f

Ctrl-L (form feed)

Ctrl-J (newline)

Ctrl-M (carriage return)

Ctrl-I (tab)

v

Ctrl-K (vertical tab)

nnn

Character with octal value nnn

\

Literal backslash

char1-char2

All characters in the range char1 through char2. If char1 does not sort before char2, produce an error.

[char*]

In string2, expand char to the length of string1.

[char*number]

Expand char to number occurrences. [x*4] expands to xxxx, for instance.

[:class:]

Expand to all characters in class, where class can be:

alnum

Letters and digits

alpha

Letters

blank

Whitespace

cntrl

Control characters

digit

Digits

graph

Printable characters except space

lower

Lowercase letters

print

Printable characters

punct

Punctuation

space

Whitespace (horizontal or vertical)

upper

Uppercase letters

xdigit

Hexadecimal digits

[=char=]

The class of characters to which char belongs.

Examples

Change uppercase to lowercase in a file:

cat file | tr 'A-Z' 'a-z'

Turn spaces into newlines (ASCII code 012):

tr ' ' ' ' < file

Strip blank lines from file and save in new.file (or use 011 to change successive tabs into one tab):

cat file | tr -s "" " " > new.file

Delete colons from file and save result in new.file:

tr -d : < file > new.file