tr命令详解

tr Linux 操作系统非常实用的工具,其实用 tr 做到的事情,几乎都可用 sed 做到,你可以把 tr 当作是实现 sed 许多基本功能的sed“简体”。可以认为tr是简单的“ 字符 ”处理工具,而sed是功能非常强大的“ 字符串 ”处理工具。<wbr style="line-height:25px"><div style="line-height:25px"> <span style="line-height:25px">【</span>注意<span style="line-height:25px">】</span><span style="color:#ff6600; line-height:25px"><span style="line-height:25px">tr</span>是单个字符处理工具,而不是字符串处理工具!</span> </div> <div style="line-height:25px"><span style="line-height:25px">基本语法:</span></div> <div style="line-height:25px"> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px">Usage:</span><span style="color:#0000ff; line-height:25px">tr [OPTION]... SET1 [SET2]</span> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px">Translate, squeeze, and/or delete characters from standard input,</span><span style="line-height:25px; color:rgb(51,102,255)">writing to standard output.</span> </div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px">tr</span><span style="color:#000080; line-height:25px">用来从标准输入中通过替换或删除操作进行字符转换。</span><span style="color:#0000ff; line-height:25px">tr</span><span style="color:#000080; line-height:25px">主要用于删除文件中控制字符或进行字符转换。使用tr时要转换两个字符串(</span><span style="color:#0000ff; line-height:25px">SET1和SET2</span><span style="color:#000080; line-height:25px">):</span><span style="color:#0000ff; line-height:25px">SET1</span><span style="color:#000080; line-height:25px">用于表示对什么字符进行替换操作,</span><span style="color:#0000ff; line-height:25px">SET2</span><span style="color:#000080; line-height:25px">用于表示</span><span style="color:#0000ff; line-height:25px">SET1</span><span style="color:#000080; line-height:25px">中的字符替换成相应的什么字符。</span> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">-c, -C</span><span style="color:#3366ff; line-height:25px">, --complement use the complement of SET1</span> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">-d</span><span style="color:#3366ff; line-height:25px">, --delete delete characters in SET1, do not translate</span> </div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">-s</span><span style="color:#3366ff; line-height:25px">, --squeeze-repeats replace each input sequence of a repeated character</span><span style="line-height:25px; color:rgb(51,102,255)"> that is listed in SET1 with a single occurrence</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> of that character</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">-t</span><span style="color:#3366ff; line-height:25px">, --truncate-set1 first truncate SET1 to length of SET2</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> --help display this help and exit</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> --version output version information and exit</span></div> </div> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#003366; line-height:25px">比较实用的模板如下这些:</span></div> <div style="line-height:25px"> <span style="line-height:25px">1、</span>将文件file中出现的"abc"替换为"xyz"</div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">cat file | tr "abc" "xyz" &gt; new_file</span></div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">这个命令表示:凡是在file中出现的"a"字母,都替换成"x"字母,"b"字母替换为"y"字母,"c"字母替换为"z"字母。而不是将字符串"abc"替换为字符串"xyz"。</span></div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px">比如,对于1.txt有字符串aabbccabc,那么运行</span><span style="color:#0000ff; line-height:25px">cat 1.txt|tr "abc" "xyz"&gt;2.txt</span><span style="color:#000080; line-height:25px">命令后,在2.txt中,其对应的字符串将变为xxyyzzxyz</span> </div> <div style="line-height:25px"> <span style="line-height:25px">2、</span>使用tr命令“统一”字母大小写</div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">(小写 --&gt; 大写)</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">cat file | tr a-z A-Z &gt; new_file</span></div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">(大写 --&gt; 小写)</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">cat file | tr A-Z a-z &gt; new_file</span></div> <div style="line-height:25px"> <span style="line-height:25px">3、</span>把文件中的数字0-9替换为a-j</div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">cat file | tr [0-9] [a-j] &gt; new_file</span></div> <div style="line-height:25px"> <span style="line-height:25px">4、</span>删除文件file中出现的"Snail"字符</div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">cat file | tr -d "Snail" &gt; new_file</span></div> <div style="line-height:25px">【注意】这里,凡是在file文件中出现的'S','n','a','i','l'字符都会被删除!而不是紧紧删除出现的"Snail”字符串。</div> <div style="line-height:25px"> <span style="line-height:25px">5、</span>删除文件file中出现的换行'\n'、制表'\t'字符</div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">cat file | tr -d "\n\t" &gt; new_file</span></div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">不可见字符都得用转义字符来表示的,这个都是统一的。</span></div> <div style="line-height:25px"> <span style="line-height:25px">6、</span>删除“<span style="color:#0000ff; line-height:25px">连续</span>着的”重复字母,只保留第一个</div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">cat file | tr -s [a-zA-Z] &gt; new_file</span></div> <div style="line-height:25px"> <span style="line-height:25px">7</span>、删除空行</div> <div style="line-height:25px"><span style="line-height:25px; color:rgb(0,0,128)">cat file | tr -s "\n" &gt; new_file</span></div> <div style="line-height:25px"> <span style="line-height:25px">8</span>、处理换行符</div> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#000080; line-height:25px">当您在Linux中使用 Mac OS 或 DOS/Windows 机器上创建的文本文件时,您会发现tr非常有用。</span></div> <div style="line-height:25px"><span style="color:#000080; line-height:25px"> 如果没有将文件保存为使用 UNIX 换行符来表示行结束这种格式,则需要将这样的文件转换成本机 UNIX 格式,否则一些命令实用程序不会正确地处理这些文件。Mac OS 的行尾以回车字符(\r)结束,许多文本处理工具将这样的文件作为一行来处理。为了纠正这个问题,可以用下列技巧:</span></div> <div style="line-height:25px; color:rgb(0,0,255)">Mac -&gt; UNIX:tr "\r" "\n"&lt;macfile &gt; unixfile</div> <div style="line-height:25px; color:rgb(0,0,255)">UNIX -&gt; Mac:tr "\n" "\r"&lt;unixfile &gt; macfile</div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">Microsoft DOS/Windows 约定,文本的每行以回车字符(\r)并后跟换行符(\n)结束。为了纠正这个问题,可以使用下列命令:</span></div> <div style="line-height:25px; color:rgb(0,0,255)">DOS/Windows -&gt; UNIX:tr -d "\r"&lt;dosfile &gt; unixfile</div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(0,0,255)">UNIX -&gt; DOS/Windows:</span><span style="color:#000080; line-height:25px">在这种情况下,需要用awk,因为tr不能插入两个字符来替换一个字符。要使用的 awk 命令为 awk '{ print $0"\r" }'&lt;unixfile &gt; dosfile</span> </div> </div> <div style="line-height:25px">【注意】这里-s后面是两个参数"\r"和"\n",用后者替换前者</div> <div style="line-height:25px"> <span style="line-height:25px">9</span>、用空格符\040替换制表符\011</div> <div style="line-height:25px"><span style="color:#000080; line-height:25px"># cat file | tr -s "\011" "\040" &gt; new_file</span></div> <div style="line-height:25px"> <span style="line-height:25px">10</span>、把路径变量中的冒号":",替换成换行符"\n"</div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">echo $PATH | tr -s ":" "\n"</span></div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">这样看到的路径变量是不是更清晰,通过本文你就了解Linux操作系中的tr命令。</span></div> </div> </wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值