![6f697a8d2f408ff490650c2b6c458deb.png](https://img-blog.csdnimg.cn/img_convert/6f697a8d2f408ff490650c2b6c458deb.png)
1.tr
tr用于替换文本文件中的字符,格式为"tr [要替换的字符] [替换后的字符]"
tr不能用于替换完整字符串,只能替换对应的字符
![a3f9af02fdd44b7a28c9f0efc30a1e15.png](https://img-blog.csdnimg.cn/img_convert/a3f9af02fdd44b7a28c9f0efc30a1e15.png)
将小写都替换成大写"cat tr.txt|tr [a-z] [A-Z]"
![4e68b57cc2bdc8d27b1721ffdd109d37.png](https://img-blog.csdnimg.cn/img_convert/4e68b57cc2bdc8d27b1721ffdd109d37.png)
或者反过来大写替换成小写"cat tr.txt|tr [A-Z] [a-z]"
还可以使用-d参数来删除文件里面出现的字符(cat tr.txt|tr -d "Linux")
![bb65ef73df320933f985f41726593d21.png](https://img-blog.csdnimg.cn/img_convert/bb65ef73df320933f985f41726593d21.png)
替换整个单词的时候
cat tr.txt|tr "linux" "s"
![eb30d12eb101e543e41d2f270b160db4.png](https://img-blog.csdnimg.cn/img_convert/eb30d12eb101e543e41d2f270b160db4.png)
tr替换单个字符比较方便,但是如果想匹配单词来删除的话还是要使用sed命令
2.sed
sed是一种流编辑器,是文本处理中非常常用的工具,能够完美配合正则表达式使用,这里我们就讲一下简单的使用,替换字符串,更为复杂的使用,我们之后单独再讲.
"sed 's/Linux/123/' tr.txt"
将文本中的Linux替换成123
![6be3d87322a6d424ef8c1092a46e8793.png](https://img-blog.csdnimg.cn/img_convert/6be3d87322a6d424ef8c1092a46e8793.png)
3.cut
cut命令可以按照文件的列来提取文本
这次我们使用系统保存用户信息的一个文件来做例子
先看一下文件内容
"cat /etc/passwd"
![a32980a37af865d17f34ce16b6ba442a.png](https://img-blog.csdnimg.cn/img_convert/a32980a37af865d17f34ce16b6ba442a.png)
第一列是用户名,使用cut打印第一列
"cut -d: -f1 /etc/passwd"
"-d:指定了分隔符为":"
"-f1"指定打印出第1列
![30d5ad921776386f5e40666a20f8b413.png](https://img-blog.csdnimg.cn/img_convert/30d5ad921776386f5e40666a20f8b413.png)
4.wc
wc可以用来统计一个文档的行数
"cat /etc/passwd|wc -l"
![522c8fed656452aabb27736babaa2286.png](https://img-blog.csdnimg.cn/img_convert/522c8fed656452aabb27736babaa2286.png)
5.sort
可以对文本进行排序
使用cut输出passwd文件第三列看一下
"cut -d: -f3 /etc/passwd"
![cacca3c29971f7b2754bbdb0c9c5fbe1.png](https://img-blog.csdnimg.cn/img_convert/cacca3c29971f7b2754bbdb0c9c5fbe1.png)
对它进行排序
"cut -d: -f3 /etc/passwd|sort"
![329ee6f8874a09ca5f7e169ff8e098a8.png](https://img-blog.csdnimg.cn/img_convert/329ee6f8874a09ca5f7e169ff8e098a8.png)
是按照文本第一位来进行排序的
我们加上参数"-n"来指定为数值排序
"cut -d: -f3 /etc/passwd|sort -n"
![c8aae4b6251ebe770801abd8a9c2a698.png](https://img-blog.csdnimg.cn/img_convert/c8aae4b6251ebe770801abd8a9c2a698.png)
参数"-r"可以进行倒序排列
![575879b77221fc5662f17d881119a3e4.png](https://img-blog.csdnimg.cn/img_convert/575879b77221fc5662f17d881119a3e4.png)