目錄
- 一. 項目背景
- 二. 通過 csplit 命令按照行數進行切割
- 2.1 步驟分解驗證
- 2.2 直接拆分
- 三. 文件合并后與原文件進行diff
- 3.1 通過 sed 命令進行合并
- 3.2 通過 cat 命令進行合并
一. 項目背景
?需要的問題
- 項目中需要獲取某個war產生的log文件,由于是商用環境的log,因此無法直接將log通過wincp傳輸到本地電腦中,只能通過linux命令將log文件的內容打印到控制臺上。
- 項目要求使用
Tera Term
來遠程連接到商用環境,且log體積超過10MB,直接使用cat命令輸出控制臺上的話,Tera Term
終端會崩潰。
?解決思路
- 需要有一種方式來將log文件進行切割,將一個大文件切割為多個小文件,然后逐個進行cat
?通過文件體積進行切割?
- 通過
split
命令,將文件按照體積進行切割,按照10MB一個的標準切割為若干個小文件。 - 但是由于日志中含有中文,按照體積進行切割的話,很可能最后將一個漢字切割開來,最后造成亂碼。
- 因此,通過文件體積進行切割的方式不可取。
二. 通過 csplit 命令按照行數進行切割
2.1 步驟分解驗證
?通過如下命令可以看到,文件有8.7MB
,一共有86509
行
apluser@ubuntu24-01:~/work/20250216$ ls -lh
total 8.7M
-rw-rw-r-- 1 apluser apluser 8.7M Apr 7 2024 CBC_SystemLog.2024-04-07.0.log
apluser@ubuntu24-01:~/work/20250216$
apluser@ubuntu24-01:~/work/20250216$ wc -l CBC_SystemLog.2024-04-07.0.log
86509 CBC_SystemLog.2024-04-07.0.log
?第一次拆分,將文件拆分出整數:csplit -f CBC_SystemLog_ -b "%02d.log" CBC_SystemLog.2024-04-07.0.log 86501
-f CBC_SystemLog_
:拆分完之后新文件的名稱前綴-b "%02d.log"
:拆分完之后的文件的后綴格式為2位數字86501
:原文件共有86509
行,將原文件從86501
行之后進行拆分,拆分為2個文件,分別有86500
行和9
行
# 將指定的文件從86501行之后拆分一次
apluser@ubuntu24-01:~/work/20250216$ csplit -f CBC_SystemLog_ -b "%02d.log" CBC_SystemLog.2024-04-07.0.log 86501
9115330
1069
apluser@ubuntu24-01:~/work/20250216$ ls -l
total 17812
-rw-rw-r-- 1 apluser apluser 9115330 Feb 16 14:48 CBC_SystemLog_00.log
-rw-rw-r-- 1 apluser apluser 1069 Feb 16 14:48 CBC_SystemLog_01.log
-rw-rw-r-- 1 apluser apluser 9116399 Apr 7 2024 CBC_SystemLog.2024-04-07.0.log
apluser@ubuntu24-01:~/work/20250216$
# 可以看到拆分完之后的文件的總行數和原文件的行數相同
apluser@ubuntu24-01:~/work/20250216$ wc -l *86500 CBC_SystemLog_00.log9 CBC_SystemLog_01.log86509 CBC_SystemLog.2024-04-07.0.log173018 total
apluser@ubuntu24-01:~/work/20250216$
?第二次拆分,將文件拆分為10的倍數:csplit -f new_log_file_prefix_ -b "%02d.log" tmp_00.log 8650 "{9}"
8650 "{9}"
8650
:第一個分割點,表示第一個文件包含 前 8650 行,從 第 8651 行 開始一個新文件。{9}
:表示 重復這個分割點 9 次,即 共執行 10 次分割。- 總共拆分 10 次,意味著文件會被分成 11 份。
apluser@ubuntu24-01:~/work/20250216$ csplit -f new_log_file_prefix_ -b "%02d.log" CBC_SystemLog_00.log 8650 "{9}"
937375
919866
875569
955326
901752
890427
935403
876211
946443
876837
121
apluser@ubuntu24-01:~/work/20250216$ ls -lh new_log_file_prefix_*
-rw-rw-r-- 1 apluser apluser 916K Feb 16 14:59 new_log_file_prefix_00.log
-rw-rw-r-- 1 apluser apluser 899K Feb 16 14:59 new_log_file_prefix_01.log
-rw-rw-r-- 1 apluser apluser 856K Feb 16 14:59 new_log_file_prefix_02.log
-rw-rw-r-- 1 apluser apluser 933K Feb 16 14:59 new_log_file_prefix_03.log
-rw-rw-r-- 1 apluser apluser 881K Feb 16 14:59 new_log_file_prefix_04.log
-rw-rw-r-- 1 apluser apluser 870K Feb 16 14:59 new_log_file_prefix_05.log
-rw-rw-r-- 1 apluser apluser 914K Feb 16 14:59 new_log_file_prefix_06.log
-rw-rw-r-- 1 apluser apluser 856K Feb 16 14:59 new_log_file_prefix_07.log
-rw-rw-r-- 1 apluser apluser 925K Feb 16 14:59 new_log_file_prefix_08.log
-rw-rw-r-- 1 apluser apluser 857K Feb 16 14:59 new_log_file_prefix_09.log
-rw-rw-r-- 1 apluser apluser 121 Feb 16 14:59 new_log_file_prefix_10.log
?通過行數比對,可以看到拆分后的總行數和原文件的行數相同
apluser@ubuntu24-01:~/work/20250216$ wc -l new_log_file_prefix_* CBC_SystemLog_01.log8649 new_log_file_prefix_00.log8650 new_log_file_prefix_01.log8650 new_log_file_prefix_02.log8650 new_log_file_prefix_03.log8650 new_log_file_prefix_04.log8650 new_log_file_prefix_05.log8650 new_log_file_prefix_06.log8650 new_log_file_prefix_07.log8650 new_log_file_prefix_08.log8650 new_log_file_prefix_09.log1 new_log_file_prefix_10.log9 CBC_SystemLog_01.log86509 total
apluser@ubuntu24-01:~/work/20250216$ wc -l CBC_SystemLog.2024-04-07.0.log
86509 CBC_SystemLog.2024-04-07.0.log
apluser@ubuntu24-01:~/work/20250216$
2.2 直接拆分
?設置9個分割點,共拆分10次,會產生11個拆分文件
apluser@ubuntu24-01:~/work/20250216$ csplit -f new_log_file_prefix_ -b "%02d.log" CBC_SystemLog.2024-04-07.0.log 8650 "{9}"
937375
919866
875569
955326
901752
890427
935403
876211
946443
876837
1190
apluser@ubuntu24-01:~/work/20250216$ wc -l new_log_file_prefix_*8649 new_log_file_prefix_00.log8650 new_log_file_prefix_01.log8650 new_log_file_prefix_02.log8650 new_log_file_prefix_03.log8650 new_log_file_prefix_04.log8650 new_log_file_prefix_05.log8650 new_log_file_prefix_06.log8650 new_log_file_prefix_07.log8650 new_log_file_prefix_08.log8650 new_log_file_prefix_09.log10 new_log_file_prefix_10.log86509 total
三. 文件合并后與原文件進行diff
?我們可以通過將拆分后的文件合并為一個文件和原文件進行diff
比較,從而驗證我們的拆分是沒有問題的。
注意,一定要按照順序來合并文件,否則diff
的時候會出現差分。
3.1 通過 sed 命令進行合并
sed ''
:''
代表空命令,即 sed 不會對文本執行任何修改,只會原樣輸出文件內容。
apluser@ubuntu24-01:~/work/20250216$ sed '' new_log_file_prefix_*.log CBC_SystemLog_01.log > merged_log_file.log
apluser@ubuntu24-01:~/work/20250216$
apluser@ubuntu24-01:~/work/20250216$ diff merged_log_file.log CBC_SystemLog.2024-04-07.0.log
apluser@ubuntu24-01:~/work/20250216$
3.2 通過 cat 命令進行合并
- 💥通常情況下
cat
的合并速度更快,盡量使用cat命令
apluser@ubuntu24-01:~/work/20250216$ cat new_log_file_prefix_*.log CBC_SystemLog_01.log > merged_log_file_tmp.log
apluser@ubuntu24-01:~/work/20250216$
apluser@ubuntu24-01:~/work/20250216$ diff merged_log_file_tmp.log CBC_SystemLog.2024-04-07.0.log
apluser@ubuntu24-01:~/work/20250216$