Linux如何對(duì)文件進(jìn)行分割和重組
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