tr替换:
tr 替换字符:被替换的字符与替换的字符数必须是相等的
用法:tr 旧字符 新字符
cat > a.txt <<EOF
root:1:0:bin
bin:2:200:bash
jim:10:300:bash
tom:102:10:bash
bin:2:200:bash
EOF
cat a.txt | tr 20 89 将cat a.txt中的结果中的20替换成89
root:1:9:bin
bin:8:899:bash
jim:19:399:bash
tom:198:19:bash
bin:8:899:bash
cat a.txt | tr ‘a-z‘ ‘A-Z‘ 在替换的时候是一一对应的替换
ROOT:1:0:BIN
BIN:2:200:BASH
JIM:10:300:BASH
TOM:102:10:BASH
BIN:2:200:BASH
cat a.txt | tr 0-4 5-9
root:6:5:bin
bin:7:755:bash
jim:65:855:bash
tom:657:65:bash
bin:7:755:bash
\n 换行符
cat a.txt | tr "\n" "+" 将换行符换成+号
root:1:0:bin+bin:2:200:bash+jim:10:300:bash+tom:102:10:bash+bin:2:200:bash+
cat a.txt | tr -s "o0" 用-s把连续重复的字符替换成一个字符
rot:1:0:bin
bin:2:20:bash
jim:10:30:bash
tom:102:10:bash
bin:2:20:bash
echo >> a.txt 在a.txt文件中追加空白行,请执行3次
cat a.txt | tr -s "\n" 去掉空白行
原文地址:http://blog.51cto.com/13553337/2088278