Linux如何对文件进行分割和重组
创始人
2025-07-08 20:01:39
0

csplit,split 和 cat 来重新整理文件,然后再将文件合并在一起。这些操作在任何文件类型下都有用:文本、图片、音频文件、ISO 镜像文件等。

使用 csplit 分割文件

csplit 将单个文件分割成多个文件。

[root@k8s-master-node1 test]# cat 1
1
2
3
4
5
6
[root@k8s-master-node1 test]#

它将文件 1 分为三个文件,以行号 2 和 5 作为分割点

[root@k8s-master-node1 test]# csplit 1 2 5
2
6
5
[root@k8s-master-node1 test]#

csplit 在当前目录下创建了三个新文件,并以字节为单位打印出新文件的大小。默认情况下,每个新文件名为 xx_nn:

[root@k8s-master-node1 test]# ls
1 xx00 xx01 xx02
[root@k8s-master-node1 test]#

分别查看内容

[root@k8s-master-node1 test]# cat xx00
1
[root@k8s-master-node1 test]# cat xx01
2
3
4
[root@k8s-master-node1 test]# cat xx02
5
6

[root@k8s-master-node1 test]#

如果要将文件分割成包含相同行数的多个文件如何操作呢?

可以指定行数,然后将重复次数放在在花括号中。此示例重复分割 4 次,并将剩下的转储到最后一个文件中

[root@k8s-master-node1 test]# csplit 1 1 {4}
0
2
2
2
2
5

可以使用星号通配符来告诉 csplit 尽可能多地重复分割。这听起来很酷,但是如果文件不能等分,则会失败(低版本的 csplit 不支持此参数)

[root@k8s-master-node1 test]# csplit 1 1 {*}
0
2
2
2
2
2
2
1
csplit: ‘1’: line number out of range on repetition 7
[root@k8s-master-node1 test]# cat 1 |wc -l
7
[root@k8s-master-node1 test]#

默认的行为是删除发生错误时的输出文件。

可以用 -k 选项来解决这个问题,当有错误时,它就不会删除输出文件。

另一个行为是每次运行 csplit 时,它将覆盖之前创建的文件,如果需要使用新的文件名来分别保存它们。可以使用使用 --prefix= prefix 来设置一个不同的文件前缀:

[root@k8s-master-node1 test]# csplit -k --prefix=mine 1 1 {*}
0
2
2
2
2
2
2
1
csplit: ‘1’: line number out of range on repetition 7
[root@k8s-master-node1 test]# ls
1 mine00 mine01 mine02 mine03 mine04 mine05 mine06 mine07
[root@k8s-master-node1 test]#

-n 可用于改变对文件进行编号的数字位数(默认是 2 位):

[root@k8s-master-node1 test]# ls
1 mine0 mine1 mine2 mine3 mine4 mine5 mine6 mine7
[root@k8s-master-node1 test]#

csplit 中的 “c” 是上下文(context)的意思。也就是说可以根据任意匹配的方式或者巧妙的正则表达式来分割文件。

下面的例子将文件分为两部分。第一个文件在包含第一次出现 “3” 的前一行处结束,第二个文件则以包含 “3” 的行开头。

[root@k8s-master-node1 test]# csplit 1 /3/
4
9
[root@k8s-master-node1 test]# ls
1  xx00  xx01
[root@k8s-master-node1 test]# cat xx00
1
2
[root@k8s-master-node1 test]# cat xx01
3
4
5
6

[root@k8s-master-node1 test]#

在每次出现 “3” 时分割文件:

[root@k8s-master-node1 test]# cat 1
1
2
3
3
4
5
6
[root@k8s-master-node1 test]#
[root@k8s-master-node1 test]# csplit 1 /3/ {*}
4
2
9
[root@k8s-master-node1 test]# ls
1  xx00  xx01  xx02
[root@k8s-master-node1 test]# cat xx00
1
2
[root@k8s-master-node1 test]# cat xx01
3
[root@k8s-master-node1 test]# cat xx02
3
4
5
6

[root@k8s-master-node1 test]#

{}  里面*可以替换为具体的数字,表述第几次出现的时候开始切割

仅当内容以包含 “3” 的行开始时才复制,并且省略前面的所有内容:

[root@k8s-master-node1 test]# cat 1
1
2
1 3
4
5
6

[root@k8s-master-node1 test]# csplit 1 %3%
11
[root@k8s-master-node1 test]# ls
1  xx00
[root@k8s-master-node1 test]# cat xx00
1 3
4
5
6

[root@k8s-master-node1 test]#

将文件分割成不同大小

split 与 csplit 类似。它将文件分割成特定的大小,当您将大文件分割成小的多媒体文件或者使用网络传送时,速度便会快很多。

默认的大小为 1000 行

# split 1.mv
# ls -hl
266K Aug 21 16:58 xaa
267K Aug 21 16:58 xab
315K Aug 21 16:58 xac
[...]

它们分割出来的大小相似,但你可以指定任何你想要的大小。这个例子中是 10M 字节

# split -b 10M 1.mv

尺寸单位缩写为 K,M,G,T,P,E,Z,Y(1024 的幂)或者 KB,MB,GB 等等(1000 的幂)。

为文件名自定义前缀和后缀:

# split -a 3 --numeric-suffixes=9 --additional-suffix=mine 1.mv SB
240K Aug 21 17:44 SB009mine
214K Aug 21 17:44 SB010mine
220K Aug 21 17:44 SB011mine

-a 选项控制编号的数字位置。

--numeric-suffixes 设置编号的开始值。默认前缀为 x,你也可以通过在文件名后输入它来设置一个不同的前缀。

将分割后的文件合并

你可能想在某个时候重组你的文件。常用的 cat 命令就用在这里:

# cat SB0* > 2.txt

示例中的星号通配符将匹配到所有以 SB0 开头的文件,但是结果可能有排查。所以使用问号通配符进行更精确的匹配,每个字符使用一个问号:

# cat SB0?????? > 2.txt


相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
P2P的自白|我不生产内容,我... 现在一提起P2P,人们就会联想到正在被有关部门“围剿”的互联网理财服务。×租宝事件使得劳...
Intel将Moblin社区控... 本周二,非营利机构Linux基金会宣布,他们将担负起Moblin社区的管理工作,而这之前,Mobli...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...