Linux csplit 命令实现日志文件的拆分
目录
- 一. 项目背景
- 二. 通过 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.log
9 CBC_SystemLog_01.log
86509 CBC_SystemLog.2024-04-07.0.log
173018 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.log
8649 new_log_file_prefix_00.log
8650 new_log_file_prefix_01.log
8650 new_log_file_prefix_02.log
8650 new_log_file_prefix_03.log
8650 new_log_file_prefix_04.log
8650 new_log_file_prefix_05.log
8650 new_log_file_prefix_06.log
8650 new_log_file_prefix_07.log
8650 new_log_file_prefix_08.log
8650 new_log_file_prefix_09.log
1 new_log_file_prefix_10.log
9 CBC_SystemLog_01.log
86509 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.log
8650 new_log_file_prefix_01.log
8650 new_log_file_prefix_02.log
8650 new_log_file_prefix_03.log
8650 new_log_file_prefix_04.log
8650 new_log_file_prefix_05.log
8650 new_log_file_prefix_06.log
8650 new_log_file_prefix_07.log
8650 new_log_file_prefix_08.log
8650 new_log_file_prefix_09.log
10 new_log_file_prefix_10.log
86509 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$