我有一个串联的日志文件,其中包含多个日志,我试图将其解析为单独的日志文件。稍后我会将它们重命名为每个的日期/时间。每个日志由“--- LOG REPORT ---”分隔。
到目前为止我有:
sed -n '/--- LOG REPORT ---/,/--- LOG REPORT ---/p' logname.log > test.out
但是,正如您可以想象的那样,这只输出模式的第一个实例。我查看了 sed 的手册页,我不相信它可以输出多个文件。也许我可以继续从文件中提取直到它为空,但这似乎工作量太大了。我怎样才能做到这一点?也许我应该改用 awk ?
输入文件 filename.log 的示例
--- LOG REPORT ---
Mary
Had
A
Little
Lamb
--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow
期望的输出:
在文件名_1.log中
--- LOG REPORT ---
Mary
Had
A
Little
Lamb
在文件名_2.log中
--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow
答案1
这个命令怎么样?
csplit logname.log /---\ LOG\ REPORT\ ---/ {*}
测试
cat logname.log
--- LOG REPORT ---
Mary
Had
A
Little
Lamb
--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow
运行上述命令后,我得到的输出是,
cat xx01
--- LOG REPORT ---
Mary
Had
A
Little
Lamb
cat xx02
--- LOG REPORT ---
Her
Fleece
Was
White
As
Snow
答案2
怎么样
awk '/--- LOG REPORT ---/ {n++;next} {print > "test"n".out"}' logname.log
答案3
错误的工具,使用 perl 或 awk 例如
cat > splitFileByLogReport.pl <<EOF
#!/usr/bin/perl
undef $/;
$_ = <>;
$n = 0;
for $match (split(/(?=--- LOG REPORT ---)/)) {
open(O, ">$ARGV[$argnum]" . ++$n);
print O $match;
close(O);
}
末梢血
然后运行:
perl splitFileByLogReport.pl yourFile.txt