-2

Input 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

Desired output files

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
terdon
  • 234,489
  • 66
  • 447
  • 667
Auro
  • 3
  • 1
  • 2
    Out of interest, why did you capitalize the words under and shell but not Linux? –  Feb 14 '17 at 13:49

1 Answers1

3

Use split:

$ split -l 5 file.txt test

This will create the three files testa, testb and testc, each with 5 consecutive lines from the file file.txt.

Alternatively, a solution in awk that writes to a new file whenever a new test case is found:

$ awk '/^Start of test case:/ { c++ } { print >sprintf("test%d.txt", c) }' file.txt
Kusalananda
  • 320,670
  • 36
  • 633
  • 936