用于管理大文件的 Linux 拆分和连接命令示例

举报
Tiamo_T 发表于 2022/07/12 19:27:01 2022/07/12
【摘要】 本文通过描述性示例解释了如何使用 Linux 拆分和连接命令。

Linux split 和 join 命令在处理大文件时非常有用。本文通过描述性示例解释了如何使用 Linux 拆分和连接命令。

加入和拆分命令语法:

join [OPTION]… FILE1 FILE2
split [OPTION]… [INPUT [PREFIX]]

Linux 拆分命令示例

1. 基本拆分示例

这是拆分命令的基本示例。

$ split split.zip 

$ ls
split.zip  xab  xad  xaf  xah  xaj  xal  xan  xap  xar  xat  xav  xax  xaz  xbb  xbd  xbf  xbh  xbj  xbl  xbn
xaa        xac  xae  xag  xai  xak  xam  xao  xaq  xas  xau  xaw  xay  xba  xbc  xbe  xbg  xbi  xbk  xbm  xbo

所以我们看到文件 split.zip 被分割成更小的文件,以 x** 作为文件名。其中 ** 是默认添加的两个字符后缀。此外,默认情况下,每个 x** 文件将包含 1000 行。

$ wc -l *
   40947 split.zip
    1000 xaa
    1000 xab
    1000 xac
    1000 xad
    1000 xae
    1000 xaf
    1000 xag
    1000 xah
    1000 xai

所以上面的输出确认默认情况下每个 x** 文件包含 1000 行。

2.使用 -a 选项更改后缀长度

如上面示例 1 中所述,默认后缀长度为 2。但这可以通过使用 -a 选项进行更改。

正如您在以下示例中看到的,它在拆分文件上使用长度为 5 的后缀。

$ split -a5 split.zip
$ ls
split.zip  xaaaac  xaaaaf  xaaaai  xaaaal  xaaaao  xaaaar  xaaaau  xaaaax  xaaaba  xaaabd  xaaabg  xaaabj  xaaabm
xaaaaa     xaaaad  xaaaag  xaaaaj  xaaaam  xaaaap  xaaaas  xaaaav  xaaaay  xaaabb  xaaabe  xaaabh  xaaabk  xaaabn
xaaaab     xaaaae  xaaaah  xaaaak  xaaaan  xaaaaq  xaaaat  xaaaaw  xaaaaz  xaaabc  xaaabf  xaaabi  xaaabl  xaaabo

3.使用-b选项自定义分割文件大小

可以使用 -b 选项控制每个输出拆分文件的大小。

在此示例中,创建的拆分文件大小为 200000 字节。

$ split -b200000 split.zip 

$ ls -lart
total 21084
drwxrwxr-x 3 himanshu himanshu     4096 Sep 26 21:20 ..
-rw-rw-r-- 1 himanshu himanshu 10767315 Sep 26 21:21 split.zip
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xad
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xac
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xab
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xaa
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xah
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xag
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xaf
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xae
-rw-rw-r-- 1 himanshu himanshu   200000 Sep 26 21:35 xar

4. 使用 -d 选项创建带有数字后缀的拆分文件

如上例所示,输出格式为 x**,其中 ** 是字母。您可以使用 -d 选项将其更改为数字。

这是一个例子。这在拆分文件上有数字后缀。

$ split -d split.zip
$ ls
split.zip  x01  x03  x05  x07  x09  x11  x13  x15  x17  x19  x21  x23  x25  x27  x29  x31  x33  x35  x37  x39
x00        x02  x04  x06  x08  x10  x12  x14  x16  x18  x20  x22  x24  x26  x28  x30  x32  x34  x36  x38  x40

5. 使用 -C 选项自定义拆分块的数量

要控制块的数量,请使用 -C 选项。

此示例将创建 50 个拆分文件块。

$ split -n50 split.zip
$ ls
split.zip  xac  xaf  xai  xal  xao  xar  xau  xax  xba  xbd  xbg  xbj  xbm  xbp  xbs  xbv
xaa        xad  xag  xaj  xam  xap  xas  xav  xay  xbb  xbe  xbh  xbk  xbn  xbq  xbt  xbw
xab        xae  xah  xak  xan  xaq  xat  xaw  xaz  xbc  xbf  xbi  xbl  xbo  xbr  xbu  xbx

6. 使用 -e 选项避免零大小的块

在将相对较小的文件拆分为大量块时,最好避免零大小的块,因为它们不会增加任何价值。这可以使用 -e 选项来完成。

这是一个例子:

$ split -n50 testfile

$ ls -lart x*
-rw-rw-r-- 1 himanshu himanshu 0 Sep 26 21:55 xag
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:55 xaf
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:55 xae
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:55 xad
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:55 xac
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:55 xab
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:55 xaa
-rw-rw-r-- 1 himanshu himanshu 0 Sep 26 21:55 xbx
-rw-rw-r-- 1 himanshu himanshu 0 Sep 26 21:55 xbw
-rw-rw-r-- 1 himanshu himanshu 0 Sep 26 21:55 xbv

所以我们看到在上面的输出中产生了很多零大小的块。现在,让我们使用 -e 选项并查看结果:

$ split -n50 -e testfile
$ ls
split.zip  testfile  xaa  xab  xac  xad  xae  xaf

$ ls -lart x*
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:57 xaf
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:57 xae
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:57 xad
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:57 xac
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:57 xab
-rw-rw-r-- 1 himanshu himanshu 1 Sep 26 21:57 xaa

所以我们看到在上面的输出中没有产生零大小的块。

7. 使用 -l 选项自定义行数

可以使用 -l 选项自定义每个输出拆分文件的行数。

如下例所示,分割文件由 20000 行创建。

$ split -l20000 split.zip

$ ls
split.zip  testfile  xaa  xab  xac

$ wc -l x*
   20000 xaa
   20000 xab
     947 xac
   40947 total

使用 –verbose 选项获取详细信息

要在每次打开新的拆分文件时获取诊断消息,请使用 –verbose 选项,如下所示。

$ split -l20000 --verbose split.zip
creating file `xaa'
creating file `xab'
creating file `xac'

Linux 加入命令示例

8. 基本连接示例

Join 命令通过匹配第一个字段来处理两个文件的第一个字段(作为输入提供)。

例子:

$ cat testfile1
1 India
2 US
3 Ireland
4 UK
5 Canada

$ cat testfile2
1 NewDelhi
2 Washington
3 Dublin
4 London
5 Toronto

$ join testfile1 testfile2
1 India NewDelhi
2 US Washington
3 Ireland Dublin
4 UK London
5 Canada Toronto

所以我们看到一个包含国家的文件与另一个包含大写字母的文件在第一个字段的基础上连接在一起。

9. 加入排序列表的工作

如果提供给 join 命令的两个文件中的任何一个未排序,则它会在输出中显示警告,并且该特定条目未连接。

在此示例中,由于输入文件未排序,因此将显示警告/错误消息。

$ cat testfile1
1 India
2 US
3 Ireland
5 Canada
4 UK

$ cat testfile2
1 NewDelhi
2 Washington
3 Dublin
4 London
5 Toronto

$ join testfile1 testfile2
1 India NewDelhi
2 US Washington
3 Ireland Dublin
join: testfile1:5: is not sorted: 4 UK
5 Canada Toronto

10. 使用 -i 选项忽略大小写

比较字段时,可以使用 -i 选项忽略大小写的差异,如下所示。

$ cat testfile1
a India
b US
c Ireland
d UK
e Canada

$ cat testfile2
a NewDelhi
B Washington
c Dublin
d London
e Toronto

$ join testfile1 testfile2
a India NewDelhi
c Ireland Dublin
d UK London
e Canada Toronto

$ join -i testfile1 testfile2
a India NewDelhi
b US Washington
c Ireland Dublin
d UK London
e Canada Toronto

11. 使用 –check-order 选项验证输入是否已排序

这是一个例子。由于 testfile1 在最后未排序,因此在输出中产生了错误。

$ cat testfile1
a India
b US
c Ireland
d UK
f Australia
e Canada

$ cat testfile2
a NewDelhi
b Washington
c Dublin
d London
e Toronto

$ join --check-order testfile1 testfile2
a India NewDelhi
b US Washington
c Ireland Dublin
d UK London
join: testfile1:6: is not sorted: e Canada

12.不要使用–nocheck-order选项检查排序

这与前面的例子相反。在这个例子中没有检查排序,也不会显示任何错误信息。

$ join --nocheck-order testfile1 testfile2
a India NewDelhi
b US Washington
c Ireland Dublin
d UK London

13. 使用 -a 选项打印不可配对的行

如果两个输入文件都不能一对一映射,那么通过 -a[FILENUM] 选项,我们可以在比较时获得那些无法配对的行。FILENUM 是文件编号(1 或 2)。

在下面的示例中,我们看到使用 -a1 生成了 testfile1 中的最后一行(在下面标记为粗体),它在 testfile2 中没有对。

$ cat testfile1
a India
b US
c Ireland
d UK
e Canada
f Australia

$ cat testfile2
a NewDelhi
b Washington
c Dublin
d London
e Toronto

$ join testfile1 testfile2
a India NewDelhi
b US Washington
c Ireland Dublin
d UK London
e Canada Toronto

$ join -a1 testfile1 testfile2
a India NewDelhi
b US Washington
c Ireland Dublin
d UK London
e Canada Toronto
f Australia

14. 使用 -v 选项仅打印未配对的行

在上面的示例中,输出中产生了成对和非成对线。但是,如果只需要未配对的输出,则使用 -v 选项,如下所示。

$ join -v1 testfile1 testfile2
f Australia

15. 使用 -1 和 -2 选项基于两个文件的不同列进行连接

默认情况下,两个文件中的第一列用于在加入之前进行比较。您可以使用 -1 和 -2 选项更改此行为。

在以下示例中,将 testfile1 的第一列与 testfile2 的第二列进行比较以生成连接命令输出。

$ cat testfile1
a India
b US
c Ireland
d UK
e Canada

$ cat testfile2
NewDelhi a
Washington b
Dublin c
London d
Toronto e

$ join -1 1 -2 2 testfile1 testfile2
a India NewDelhi
b US Washington
c Ireland Dublin
d UK London
e Canada Toronto
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。