输入file.txt
:
Start of test case:test1
a
b
c
Date is feb 12
Start of test case:test2
m
n
o
Date is feb 13
Start of test case:test3
x
y
z
Date is feb 14
所需的输出文件
test1.txt
:
Start of test case:test1
a
b
c
Date is feb 12
test2.txt
:
Start of test case:test2
m
n
o
Date is feb 13
test3.txt
:
Start of test case:test3
x
y
z
Date is feb 14
答案1
使用split
:
$ split -l 5 file.txt test
这将创建三个文件testa
,testb
并且testc
每个文件包含该文件中的 5 个连续行file.txt
。
awk
或者,每当发现新的测试用例时写入新文件的解决方案:
$ awk '/^Start of test case:/ { c++ } { print >sprintf("test%d.txt", c) }' file.txt