在linux Shell下分割文件

在linux Shell下分割文件

输入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

这将创建三个文件testatestb并且testc每个文件包含该文件中的 5 个连续行file.txt

awk或者,每当发现新的测试用例时写入新文件的解决方案:

$ awk '/^Start of test case:/ { c++ } { print >sprintf("test%d.txt", c) }' file.txt

相关内容