自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux如何對(duì)文件進(jìn)行分割和重組

系統(tǒng) Linux
csplit 中的 “c” 是上下文(context)的意思。也就是說可以根據(jù)任意匹配的方式或者巧妙的正則表達(dá)式來分割文件。

csplit,split 和 cat 來重新整理文件,然后再將文件合并在一起。這些操作在任何文件類型下都有用:文本、圖片、音頻文件、ISO 鏡像文件等。

使用 csplit 分割文件

csplit 將單個(gè)文件分割成多個(gè)文件。

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

它將文件 1 分為三個(gè)文件,以行號(hào) 2 和 5 作為分割點(diǎn)

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

csplit 在當(dāng)前目錄下創(chuàng)建了三個(gè)新文件,并以字節(jié)為單位打印出新文件的大小。默認(rèn)情況下,每個(gè)新文件名為 xx_nn:

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

分別查看內(nèi)容

[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]#

如果要將文件分割成包含相同行數(shù)的多個(gè)文件如何操作呢?

可以指定行數(shù),然后將重復(fù)次數(shù)放在在花括號(hào)中。此示例重復(fù)分割 4 次,并將剩下的轉(zhuǎn)儲(chǔ)到最后一個(gè)文件中

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

可以使用星號(hào)通配符來告訴 csplit 盡可能多地重復(fù)分割。這聽起來很酷,但是如果文件不能等分,則會(huì)失?。ǖ桶姹镜?csplit 不支持此參數(shù))

[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]#

默認(rèn)的行為是刪除發(fā)生錯(cuò)誤時(shí)的輸出文件。

可以用 -k 選項(xiàng)來解決這個(gè)問題,當(dāng)有錯(cuò)誤時(shí),它就不會(huì)刪除輸出文件。

另一個(gè)行為是每次運(yùn)行 csplit 時(shí),它將覆蓋之前創(chuàng)建的文件,如果需要使用新的文件名來分別保存它們。可以使用使用 --prefix= prefix 來設(shè)置一個(gè)不同的文件前綴:

[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 可用于改變對(duì)文件進(jìn)行編號(hào)的數(shù)字位數(shù)(默認(rè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)的意思。也就是說可以根據(jù)任意匹配的方式或者巧妙的正則表達(dá)式來分割文件。

下面的例子將文件分為兩部分。第一個(gè)文件在包含第一次出現(xiàn) “3” 的前一行處結(jié)束,第二個(gè)文件則以包含 “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]#

在每次出現(xiàn) “3” 時(shí)分割文件:

[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]#

{}  里面*可以替換為具體的數(shù)字,表述第幾次出現(xiàn)的時(shí)候開始切割

僅當(dāng)內(nèi)容以包含 “3” 的行開始時(shí)才復(fù)制,并且省略前面的所有內(nèi)容:

[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 類似。它將文件分割成特定的大小,當(dāng)您將大文件分割成小的多媒體文件或者使用網(wǎng)絡(luò)傳送時(shí),速度便會(huì)快很多。

默認(rèn)的大小為 1000 行

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

它們分割出來的大小相似,但你可以指定任何你想要的大小。這個(gè)例子中是 10M 字節(jié)

# 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 選項(xiàng)控制編號(hào)的數(shù)字位置。

--numeric-suffixes 設(shè)置編號(hào)的開始值。默認(rèn)前綴為 x,你也可以通過在文件名后輸入它來設(shè)置一個(gè)不同的前綴。

將分割后的文件合并

你可能想在某個(gè)時(shí)候重組你的文件。常用的 cat 命令就用在這里:

# cat SB0* > 2.txt

示例中的星號(hào)通配符將匹配到所有以 SB0 開頭的文件,但是結(jié)果可能有排查。所以使用問號(hào)通配符進(jìn)行更精確的匹配,每個(gè)字符使用一個(gè)問號(hào):

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


責(zé)任編輯:武曉燕 來源: 步步運(yùn)維步步坑
相關(guān)推薦

2017-08-30 08:57:49

Linux分割文件重組文件

2021-12-02 08:47:40

LinuxLinux命令

2009-12-24 10:12:02

Linux查看文件編碼

2011-10-27 14:15:05

Java 7

2017-06-01 15:30:32

LinuxVim文件加密

2016-12-14 09:24:42

文件目錄壓縮

2010-03-05 09:40:08

Python遞歸

2017-08-01 17:34:47

Linux內(nèi)核驅(qū)動(dòng)文件讀寫

2015-08-25 15:53:08

LinuxcURL

2019-12-17 09:00:48

split分割Linux文件Linux

2010-11-29 14:24:06

Linux軟件管理

2011-09-13 14:25:31

Nvidia芯片移動(dòng)

2011-04-14 17:03:50

Linuxsplitcat

2011-09-07 14:43:24

2022-08-17 12:35:26

Linux sed編輯器

2010-04-30 11:22:23

Unix系統(tǒng)

2020-11-30 12:32:40

PyTorch語義分割python

2010-07-01 10:20:41

SQL Server

2021-12-04 09:10:09

可視化

2010-08-05 09:46:45

FlexAIR文件打包
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)