欢迎使用Markdown编辑器写博客
MAN tr
Unix(macbook) 对tr命令的介绍如下:
TR(1) BSD General Commands Manual TR(1)
NAME
tr -- translate characters
SYNOPSIS
tr [-Ccsu] string1 string2
tr [-Ccu] -d string1
tr [-Ccu] -s string1
tr [-Ccu] -ds string1 string2
DESCRIPTION
The tr utility copies the standard input to the standard output with substitution or deletion of
selected characters.
The following options are available:
-C Complement the set of characters in string1, that is ``-C ab'' includes every character except
for `a' and `b'.
-c Same as -C but complement the set of values in string1.
-d Delete characters in string1 from the input.
-s Squeeze multiple occurrences of the characters listed in the last operand (either string1 or
string2) in the input into a single instance of the character. This occurs after all deletion
and translation is completed.
-u Guarantee that any output is unbuffered.
In the first synopsis form, the characters in string1 are translated into the characters in string2
where the first character in string1 is translated into the first character in string2 and so on. If
string1 is longer than string2, the last character found in string2 is duplicated until string1 is
exhausted.
In the second synopsis form, the characters in string1 are deleted from the input.
In the third synopsis form, the characters in string1 are compressed as described for the -s option.
In the fourth synopsis form, the characters in string1 are deleted from the input, and the characters
in string2 are compressed as described for the -s option.
EXIT STATUS
The tr utility exits 0 on success, and >0 if an error occurs.
EXAMPLES
The following examples are shown as given to the shell:
Create a list of the words in file1, one per line, where a word is taken to be a maximal string of let-
ters.
tr -cs "[:alpha:]" "\n" < file1
Translate the contents of file1 to upper-case.
tr "[:lower:]" "[:upper:]" < file1
(This should be preferred over the traditional UNIX idiom of ``tr a-z A-Z'', since it works correctly
in all locales.)
Strip out non-printable characters from file1.
tr -cd "[:print:]" < file1
Remove diacritical marks from all accented variants of the letter `e':
tr "[=e=]" "e"
Example
Example-01 lower case to upper case
zhuqingpings-MacBook-Pro:~ zhuqingping$ cat /etc/profile
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
zhuqingpings-MacBook-Pro:~ zhuqingping$ cat /etc/profile | tr 'a-z' 'A-Z'
# SYSTEM-WIDE .PROFILE FOR SH(1)
IF [ -X /USR/LIBEXEC/PATH_HELPER ]; THEN
EVAL `/USR/LIBEXEC/PATH_HELPER -S`
FI
IF [ "${BASH-NO}" != "NO" ]; THEN
[ -R /ETC/BASHRC ] && . /ETC/BASHRC
FI
Example-02 delete some characters
zhuqingpings-MacBook-Pro:~ zhuqingping$ cat date.txt
Monday 09:00
Tuesday 09:10
Wednesday 10:11
Thursday 11:30
Friday 08:00
Saturday 07:40
Sunday 10:00
zhuqingpings-MacBook-Pro:~ zhuqingping$ cat date.txt | tr -d '[0-9][: ]'
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
zhuqingpings-MacBook-Pro:~ zhuqingping$ cat date.txt | tr -cd '[a-z][A-Z]\n'
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Example-03 delete sqeeze characters
zhuqingpings-MacBook-Pro:~ zhuqingping$ echo "thissss is a text linnnnnnne." | tr -s ' sn'
this is a text line.