前言
在 Linux 环境下,文本处理是日常操作中的重要部分。本文将详细介绍 lLinux 中常用的文本处理命令。(本文将以 Ubuntu 版本示例)
cat命令
查看一个文件
root@ubuntu:/home# cat f1
111111111111111111111
22222222222222222
333333333333333333333333
查看多个文件
root@ubuntu:/home# cat f1 f2
111111111111111111111
22222222222222222
333333333333333333333333
11111111111
222222222222
33333333333
-n 选项,显示行号
root@ubuntu:/home# cat -n f3
1 hello world
2
3 hello everyone
4 one two one
-b 选项,显示行号跳过空行
root@ubuntu:/home# cat -b f3
1 hello world
2 hello everyone
3 one two one
-A 选项,显示所有控制符
root@ubuntu:/home# cat -A f3
hello world$
$
^Ihello everyone$
one two one$
$
tac 命令
[root@ubuntu:/home]$ tac f3
one two one
hello everyone
hello world
rev 命令
[root@ubuntu:/home]$ rev f3
dlrow olleh
enoyreve olleh
eno owt eno
more 命令
[root@ubuntu:/home]$ more /var/log/syslog
查看操作 | 按键 |
---|
向下翻一页 | 空格键 |
向下滚动一行 | Enter |
向上翻一页 | b |
退出查看 | q |
less 命令
[root@ubuntu:/home]$ less /var/log/syslog
查看操作 | 按键 |
---|
向下翻一页 | 空格键 |
向下滚动一行 | Enter |
向上翻一页 | b |
向上滚动一行 | y |
跳转到文件的开头 | g |
跳转到文件的末尾 | G |
向下搜索指定模式 | /关键字 |
向上搜索指定模式 | ?关键字 |
跳转到下一个匹配模式的位置 | n |
跳转到上一个匹配模式的位置 | N |
退出查看 | q |
head 命令
-n 选项,查看前几行的内容
[root@ubuntu:/home]$ head -2 f4
边草,边草,
边草尽来兵老。
[root@ubuntu:/home]$ head -n -2 f4
边草,边草,
边草尽来兵老。
山南山北雪晴,
千里万里月明。
tail 命令
-n 选项,查看后几行的内容
[root@ubuntu:/home]$ tail -2 f4
明月,明月,
胡笳一声愁绝。
[root@ubuntu:/home]$ tail -n +2 f4
边草尽来兵老。
山南山北雪晴,
千里万里月明。
明月,明月,
胡笳一声愁绝。
-f 选项,实时显示文件新追加的内容
[root@ubuntu:/home]$ tail -fn0 /var/log/syslog
cut 命令
-d -f 选项,指定分隔符,提取指定列文本
[root@ubuntu:/home]$ cut -d: -f1,3-4,7 /etc/passwd
root:0:0:/bin/bash
daemon:1:1:/usr/sbin/nologin
[root@ubuntu:/home]$ df | tr -s " " | cut -d" " -f5 | tr -dc "[0-9\n]"
1
7
0
0
14
1
[root@ubuntu:/home]$ df | tr -s ' ' % |cut -d% -f5 |tr -d '[:alpha:]'
1
7
0
0
14
1
[root@ubuntu:~]$ df | cut -c64-65
se
1
5
0
0
6
1
[root@ubuntu:~]$ cut -d: -f1,3,7 --output-delimiter="---" /etc/passwd
root---0---/bin/bash
daemon---1---/usr/sbin/nologin
past 命令
[root@ubuntu:/home]$ paste f3 f4
hello world 边草,边草,
边草尽来兵老。
hello everyone 山南山北雪晴,
one two one 千里万里月明。
明月,明月,
胡笳一声愁绝。
[root@ubuntu:/home]$ paste -d":" f3 f4
hello world:边草,边草,
:边草尽来兵老。
hello everyone:山南山北雪晴,
one two one:千里万里月明。
:明月,明月,
:胡笳一声愁绝。
[root@ubuntu:/home]$ paste -s f4
边草,边草, 边草尽来兵老。 山南山北雪晴, 千里万里月明。 明月,明月, 胡笳一声愁绝。
wc 命令
[root@ubuntu:/home]$ wc f4
6 6 126 f4
-l 选项,仅显示行数
[root@ubuntu:/home]$ wc -l f4
6 f4
[root@ubuntu:/home]$ df | tail -n $(echo `df | wc -l`-1|bc)
tmpfs 196612 1288 195324 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 101590008 4464588 91918792 5% /
sort 命令
[root@ubuntu:/home]$ cut -d: -f1,3 /etc/passwd|sort -t: -k2 -nr |head -n3
nobody:65534
jerry:1003
tom:1002
[root@ubuntu ~]# cut -d" " -f1 /var/log/nginx/access_log |sort -u|wc -l
201
[root@ubuntu ~]# df| tr -s ' ' '%'|cut -d% -f5|sort -nr|head -1
14
思考:有两个文件,a.txt与b.txt ,合并两个文件,并输出时确保每个数字也唯一
[root@ubuntu ~]# cat a.txt b.txt | sort -n | uniq
uniq 命令
-c 选项,显示每行出现的次数
[root@ubuntu ~]# sort -n a.txt b.txt | uniq -c
1 5
2 8
2 33
[root@ubuntu2204 ~]# lastb | head -n $(echo `lastb | wc -l`-2 | bc) | tr -s ' ' | cut -d " " -f3 | sort | uniq -c | sort -nr | head -3
5 183.136.225.32
2 47.101.154.149
1 64.62.197.96
-d 选项,取两个文件相同行
[root@ubuntu ~]# cat a.txt b.txt | sort | uniq -d
-u 选项,取两个文件不同行
[root@ubuntu ~]# cat a.txt b.txt | sort | uniq -u