2 文本常见处理工具
2.1 文件内容查看命令
2.1.1 查看文本文件内容
2.1.1.1 cat
cat 可以查看文本内容
格式:
cat [OPTION]... [FILE]...
常见选项
-E:显示行结束符$
-A:显示所有控制符
-n:对显示出的每一行进行编号
-b:非空行编号
-s:压缩连续的空行成一行
范例:
[root@centos8 ~]#cat -A /data/fa.txt
a b$
c $
d^Ib^Ic$
[root@centos8 ~]#cat /data/fa.txt
a b
c
d b c
[root@centos8 ~]#cat /data/fb.txt
a
b
c
[root@centos8 ~]#hexdump -C /data/fb.txt
00000000 61 0d 0a 62 0d 0a 63 0d 0a |a..b..c..|
00000009
[root@centos8 ~]#cat -A /data/fb.txt
a^M$
b^M$
c^M$
[root@centos8 ~]#file /data/fb.txt
/data/fb.txt: ASCII text, with CRLF line terminators
2.1.1.2 nl
显示行号,相当于cat -b
[root@centos8 ~]#cat /data/f1.txt
a
b
c
d
e
f
g
h
[root@centos8 ~]#nl /data/f1.txt
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
2.1.1.3 tac
逆向显示文本内容
[root@centos8 ~]#cat /data/fa.txt
1
2
3
4
5
[root@centos8 ~]#tac /data/fa.txt
5
4
3
2
1
[root@centos8 ~]#tac
a
bb
ccc 按ctrl+d
ccc
bb
a
[root@centos8 ~]#seq 10 | tac
10
9
8
7
6
5
4
3
2
1
2.1.1.4 rev
将同一行的内容逆向显示
[root@centos8 ~]#cat /data/fa.txt
1 2 3 4 5
a b c
[root@centos8 ~]#tac /data/fa.txt
a b c
1 2 3 4 5
[root@centos8 ~]#rev /data/fa.txt
5 4 3 2 1
c b a
[root@centos8 ~]#rev
abcdef
fedcba
[root@centos8 ~]#echo {1..10} |rev
01 9 8 7 6 5 4 3 2 1
2.1.2 查看非文本文件内容
2.1.2.1 hexdump
hexdump -C -n 512 /dev/sda
00000000 eb 63 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.c..............|
echo {
a..z} | tr -d ' '|hexdump -C
00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|
00000010 71 72 73 74 75 76 77 78 79 7a 0a |qrstuvwxyz.|
0000001b
2.1.2.2 od
od 即 dump files in octal and other formats
范例:
[root@centos8 ~]#echo {a..z} | tr -d ' '|od -t x
0000000 64636261 68676665 6c6b6a69 706f6e6d
0000020 74737271 78777675 000a7a79
0000033
[root@centos8 ~]#echo {a..z} | tr -d ' '|od -t x1
0000000 61 62 63 64 65 66 67 68