## 排序显示-sort
### 作用
- sort命令用于对文本内容进行排序显示
### 格式:
```bash
sort -参数 文件名
```
### 参数
| 选项 | 作用
| ---- | --------------
| -f | 忽略大小写
| -b | 忽略缩进与空格
| -n | 以数值型排序
| -r | 反向排序
| -u | 去除重复行
| -t | 指定间隔符
| -k | 设置字段范围
### 示例
- sort命令执行后默认会按照字母顺序进行排序
```shell
[root@server ~]# cat fruit.txt
banana
pear
apple
orange
[root@server ~]# sort fruit.txt
apple
banana
orange
pear
[root@server ~]# cat /etc/passwd | cut -d : -f1 | sort # 取出所有账户名
```
- sort -u参数进行去重操作:
```shell
[root@server ~]# cat sort.txt
Welcome to openlab.com
Red Hat certified
Welcome to openlab.com
Free Linux Lessons
Linux Course
[root@server ~]# sort -u sort.txt
Free Linux Lessons
Red Hat certified
Welcome to openlab.com
```
- 数字排序
```shell
[root@server ~]# cat number.txt
45
12
3
98
82
67
24
56
9
[root@server ~]# sort -n number.txt
[root@server ~]# sort -nr number.txt # 降序
```
## 去重显示-uniq
### 作用
- uniq命令用于去除文本中连续的重复行
### 格式
```bash
uniq -参数 文件名
```
### 示例
- 对比两个文本内容进行操作
```shell
[root@server ~]# cat uq1.txt
Welcome to openlab.com
Welcome to openlab.com
Welcome to openlab.com
Welcome to openlab.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@kongd ~]# uniq uq1.txt
Welcome to openlab.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
```
## 文件中字符替换显示-tr
### 作用
- tr 指令从标准输入读取数据,经过替换或者删除后,将结果输出到标准输出
### 格式
```bash
tr -参数 替换内容 新内容
```
### 参数
- -c :反选设定字符,符合 “替换内容” 的部份不做处理,不符合的剩余部份才进行转换
- -d :删除指令字符
- -s :缩减连续重复的字符成指定的单个字符
### 示例
- 小写字母全部转换成大写字母
```shell
[root@server ~]# cat /etc/passwd | tr a-z A-Z
```
- 提取IP地址
```bash
[root@server ~]# ip a | grep ens160 | grep inet | cut -d / -f1 | tr -s ' ' | cut -d ' ' -f3
```