服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - Linux如何对文件进行分割和重组

Linux如何对文件进行分割和重组

2023-12-13 17:06未知服务器之家 Linux

csplit,split 和 cat 来重新整理文件,然后再将文件合并在一起。这些操作在任何文件类型下都有用:文本、图片、音频文件、ISO 镜像文件等。 使用 csplit 分割文件 csplit 将单个文件分割成多个文件。 [root@k8s-master-node1 test]# cat 11234

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


延伸 · 阅读

精彩推荐
  • Linuxlinux下ffmpeg的使用方法详解

    linux下ffmpeg的使用方法详解

    今天小编为大家带来的是linux下ffmpeg的使用方法详解!希望对大家会有帮助!有需要的朋友一起去看看吧...

    linux之家4462019-06-06
  • Linux系统强化 指点你怎样强化Linux安全

    系统强化 指点你怎样强化Linux安全

    这篇文章主要介绍了如何强化Linux系统安全,不过在开端强化之前,用户须要对以下三个疑问有一个清醒的看法,一个疑问是这个系统用于什么目的,二是...

    Linux教程网3852019-11-04
  • Linuxlinux命令之gzip命令详解和使用实例

    linux命令之gzip命令详解和使用实例

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。gzip是在Linux系统中经常使用的一个对文件进行...

    linux命令大全9802019-11-18
  • LinuxLinux磁盘挂载和docker安装

    Linux磁盘挂载和docker安装

    本篇给大家介绍Linux磁盘挂载和docker安装的相关内容,Linux服务器的操作是一个手熟的过程,一些不常用的命令隔断时间就忘记了,熟话说好记性不如烂笔头...

    今日头条10762021-03-29
  • LinuxLinux文件系统和卷管理器ZFS 0.8.1发布

    Linux文件系统和卷管理器ZFS 0.8.1发布

    Linux 文件系统和卷管理器 ZFS On Linux 0.8.1发布了,此版本兼容 2.6.32-5.1 Linux 内核。 更新内容包括: 修复 arc_is_overflowing() 中的比较签名 修复原始接收的错误...

    开源中国5302019-06-15
  • Linux关于进程间通信的Linux小程序

    关于进程间通信的Linux小程序

    这篇文章主要为大家详细介绍了一个关于进程间通信的Linux小程序,主要利用共享内存实现进程间通信,使用管道实现进程间竞争关系,感兴趣的朋友可以...

    net小伙9462021-11-04
  • LinuxLinux中使用ss命令检查网络连接的方法

    Linux中使用ss命令检查网络连接的方法

    这篇文章主要介绍了Linux中使用ss命令检查网络连接的方法,包括列出各种套接字,需要的朋友可以参考下 ...

    开源中文社区4542019-09-27
  • LinuxLinux系统的基本命令之逻辑命令的使用方法

    Linux系统的基本命令之逻辑命令的使用方法

    这篇文章主要为大家介绍了Linux系统的基本命令的测试使用方法,以及常用的文件测试符的意思,需要的朋友可以和脚本之家的小编一起来学习一下 ...

    Linux教程网6152019-10-31