This is easily done using a small awk script.
#!/usr/bin/awk -f
# Write sections of the input file to separate files
# Written by PM 2Ring 2016.06.14
BEGIN{outbase = "outfile"}
/^Model/{outname = outbase $2}
{print > outname}
outbase
is the base file name. It gets the Model number appended to it, so for your sample file the output files outfile1
, outfile2
, etc will get created. With a minor change to the script you could set outbase
from the command line, using awk's -v
option.
The heart of this script is
/^Model/{outname = outbase $2}
It says: If the current line starts with "Model" append the contents of field #2 to the outbase
string, assigning the result to outname
.
By default, awk process a file line by line, splitting each line into fields using whitespace as the field separator.
{print > outname}
simply prints the current line to the file whose name is stored in outname
.
This script is small enough to write the whole thing on the command line:
awk 'BEGIN{outbase = "outfile"}; /^Model/{outname = outbase $2}; {print > outname}' infile.txt
You can actually supply multiple input file arguments and they will be handled correctly, as long as you don't have duplicated Model numbers.
Model.............N
(dots are space).I'd assume OP would not want all that space in filenames. – 123 Jun 14 '16 at 11:10read
is rather slow. It obtains its input character by character, with a system call required for each character. It's really designed for interactive use, not text processing. Please see Why is using a shell loop to process text considered bad practice? for further details. – PM 2Ring Jun 14 '16 at 11:21