文件处理工具

文件处理工具

1 常见的文件处理工具

1.1 文件内容查看

1.1.1 查看文本文件内容
1.1.1.1 cat

cat 可以查看文本文件内容

cat [OPTION]..  [FILE]..

常见选项:

  • -E 显示行结束符$
  • -A 显示所有控制符
  • -n 对显示的每一行编号
  • -b 非空行编号
  • -s 压缩连续的空行成一行

范例:

root@centos8 ~]#cat -A fa.txt 
sd df dads$
a  da$
fa   d^Ig$
 a^I s$
1.1.1.2 nl

显示行号,相当于cat -b

[root@centos8 ~]#cat -b fa.txt 
     1	sd df dads
     2	a  da
     3	fa   d	g
     4	 a	 s
[root@centos8 ~]#nl fa.txt 
     1	sd df dads
     2	a  da
     3	fa   d	g
     4	 a	 s
1.1.1.3 tac

逆向显示文本内容

[root@centos8 ~]#cat fa.txt 
sd df dads
a  da
fa   d	g
 a	 s
[root@centos8 ~]#tac fa.txt
 a	 s
fa   d	g
a  da
sd df dads
1.1.1.4 rev

将同一行的内容逆向显示

root@centos8 ~]#cat fa.txt 
1 2 3 4 5 6
4 5 6 7 8 9
[root@centos8 ~]#rev fa.txt 
6 5 4 3 2 1
9 8 7 6 5 4
1.1.2 查看非文本文件内容
1.1.2.1 hexdump

范例:

[root@centos8 ~]#hexdump -C -n 10 /dev/sda
00000000  eb 63 90 10 8e d0 bc 00  b0 b8                    |.c........|
0000000a
1.1.2.2 od

范例:

[root@centos8 ~]#echo {a..h}|tr -d ' ' |od -t x
0000000 64636261 68676665 0000000a
0000011
[root@centos8 ~]#echo {a..h}|tr -d ' ' |od -t x1
0000000 61 62 63 64 65 66 67 68 0a
0000011
[root@centos8 ~]#echo {a..h}|tr -d ' ' |od -t x1z
0000000 61 62 63 64 65 66 67 68 0a                       >abcdefgh.<
0000011
1.1.2.3 xxd
[root@centos8 ~]#echo {a..h}|tr -d ' ' |xxd
00000000: 6162 6364 6566 6768 0a                   abcdefgh.

1.2 分页查看文件内容

1.2.1 more

可以实现分页查看文件,可以配合管道实现输出信息的分页

more [option] file
  • -d 显示翻页即退出提示
1.2.2 less

less 可以实现分页查看文件或stdin输出,less 命令是man命令使用的分页器
查看时有用的命令

/文本 搜索 文本
n/N 跳到下一个 或上一个匹配

1.3 显示文本前或后行内容

1.3.1 head

可以显示文件或标准输入的前面行

head [option] [file]

选项:

  • -c # 指定获取前#字节
  • -n # 指定获取前#行
  • -# 获取前#行

范例

[root@centos8 ~]#head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@centos8 ~]#cat /etc/passwd |head -n3
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
1.3.2 tail

tail和head 相反,查看文件或标准输入的倒数行

tail [option] [file]

选项

  • -c # 指定获取后#字节
  • n # 指定获取后#行
  • -f 跟踪文件新追加的内容,常用日志监控,当文件删除再新建同名文件,将无法继续跟踪文件
  • -F 跟踪文件名,当文件删除再新建同名文件,将无法继续跟踪文件
  • tailf 类似tail -f ,当文件不增长时并不访问文件
[root@centos8 ~]# tail -n 3 /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haha:x:1001:1001::/home/haha:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@centos8 ~]#cat /etc/passwd |tail -n3
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haha:x:1001:1001::/home/haha:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@centos8 ~]#tail -f /var/log/messages
Aug  7 20:25:39 centos8 NetworkManager[670]: <info>  [1596803139.4386] dhcp4 (eth0):   nameserver '10.0.0.2'
Aug  7 20:25:39 centos8 NetworkManager[670]: <info>  [1596803139.4386] dhcp4 (eth0):   domain name 'localdomain'

1.4 按列抽取文本 cut

cut 命令可以提取文本文件或stdin输入数据的指定列

cut [option] [file]

选项:

  • -d 指明分隔符,默认TAB
  • -f # :第#个字段
  • -c 按字符切割
  • --output -delimiter=STRING 指定输出分割符
[root@centos8 ~]#ifconfig |head -n2|tail -n1 |tr -s ' '|cut -d' ' -f3
10.0.0.101
[root@centos8 ~]#cut -d: -f1,3,7 --output-delimiter="--"  /etc/passwd |head -n3
root--0--/bin/bash
bin--1--/sbin/nologin
daemon--2--/sbin/nologin

1.5 合并多个文件 paste

paste 合并多个文件同行号的列在一列
格式

paste [option] [file]
  • -d 指定分割符
  • -s 所有行合成一行显示
[root@centos8 ~]#cat f1.txt
1
2
3
4
5
[root@centos8 ~]#cat f2.txt
a
b
c
[root@centos8 ~]#paste f1.txt f2.txt 
1	a
2	b
3	c
4	
5	
[root@centos8 ~]#paste -d: f1.txt f2.txt 
1:a
2:b
3:c
4:
5:
[root@centos8 ~]#paste -s f1.txt 
1	2	3	4	5

1.6 分析文本的工具

文本统计工具:wc
整理文本:sort
比较文件:diff和patch

1.6.1 收集文本统计数据 wc

wc 命令可统计文件的行总数、单词总数、字节总数和字符总数

  • -l 只计算行数
  • -w 只计算单词总数
  • -c 只计算字节总数
  • -m 只计算字符总数
  • -L 显示文件中最长行的长度
[root@centos8 ~]#wc /etc/passwd
  25   50     1217 /etc/passwd
  行数 单词数  字节数
[root@centos8 ~]#wc /etc/passwd
  25   50 1217 /etc/passwd
1.6.2 文本排序 sort

把整理过的文本显示在STDOUT,不改变原始文件

sort [option] file(s) 
  • -r 执行反方向整理
  • -R 随机排序
  • -n 执行按数字大小整理
  • -h 人类可读排序,如:2k 1G
  • -f 忽略字符串中的字符大小写
  • -u 去重
  • -t c 使用c作为字段界定符
  • -k # 使用c字符分隔得#列来整理
[root@centos8 ~]#cut -d: -f1,3 /etc/passwd |sort -t: -k2 -nr |head -n3
nobody:65534
haha:1001
meng:1000

统计分区利用率

[root@centos8 ~]#df |tail -n4 |head -n3|tr -s ' ' % |cut -d % -f5|sort -nr |head -n1
14
[root@centos8 ~]#df |tr -s ' ' %|cut -d% -f5|tr -d '[:alpha:]'|sort -nr|head -n1
14
1.6.3 去重 uniq

uniq 命令从输入中删除前后相接的重复行

uniq [option]  [file]
  • -c 显示每行重复出现的次数
  • -d 仅显示重复的行
  • -u 仅显示不重复的行
    uniq和sort命令经常配合使用
sort userlist.txt |uniq -c

范例:统计日志访问量最多的请求

[root@centos8 ~]#cut -d' ' -f1 access_log |sort|uniq -c |sort -nr|head -n3
   4870 172.20.116.228
   3429 172.20.116.208
   2834 172.20.0.222
1.6.4 比较文件
1.6.4.1 diff

diff 命令比较两个文件的区别

  • -u 输出统一的diff格式文件

范例:

[root@centos8 ~]#cat f1.txt 
a
b
12
35
vf
[root@centos8 ~]#cat f2.txt 
ab
cd
135
246
[root@centos8 ~]#diff f1.txt f2.txt 
1,5c1,4
< a
< b
< 12
< 35
< vf
---
> ab
> cd
> 135
> 246
[root@centos8 ~]#diff -u f1.txt f2.txt 
--- f1.txt	2020-08-08 09:04:14.339394104 +0800
+++ f2.txt	2020-08-08 09:04:51.419392224 +0800
@@ -1,5 +1,4 @@
-a
-b
-12
-35
-vf
+ab
+cd
+135
+246
1.6.4.2 patch

patch 复制在其他文件中进行的变化

  • -b 自动备份改变了的文件

范例 :

diff -u f1.txt f2.txt>f1_2.patch
patch -b f1.txt f1_2.patch
1.6.4.3 cmp

查看二进制文件的不同

[root@centos8 ~]#cmp /bin/dir /bin/ls
/bin/dir /bin/ls differ: byte 737, line 2
[root@centos8 ~]#hexdump -s 730 -Cn 7 /bin/dir
000002da  00 00 47 4e 55 00 b0                              |..GNU..|
000002e1
[root@centos8 ~]#hexdump -s 730 -Cn 7 /bin/ls
000002da  00 00 47 4e 55 00 93                              |..GNU..|
000002e1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值