Linux/Unix 命令 tr

欢迎使用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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值