Linux如何對文件進行分割和重組
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