Split a file based on encountering header

I need to split a file based on headers found

Input file
file1 content:

ADD
john
mickey

DROP
matt
sam

output of file F1

john
mickey

output of file F2

matt
sam

I tried this:

code:

awk '/ADD/{x="F"++i;}{print > x;}' file1

but this is writing all the records to a single file

Try

awk '$1=="ADD"{f="F1"; next} $1=="DROP"{f="F2"; next} NF{print >f}' file1

or for example:

awk '/^(ADD|DROP)/{suffix=$1; next} NF{print >(FILENAME "." suffix)}' file1

The use of the NF condition, ensures that empty lines are discarded..

Hi.

Using upper-case letters as the pattern, consider items 2 and 7 from list below:

Split a text file into pieces, i.e. groups of lines:

        1) split, standard, but many variants exist in systems
           split a file into pieces

        2) csplit, standard
           split a file into sections determined by context lines

        3) r3split (local, Ratio, iRregular, Random,
           specify numbers in groups, percentage, random set of lines)

        4) pasplit (local, approximate, fast parallel-process split)

        5) mmsplit (local, multi-method, by number of lines, groups, pattern))

        6) scatter (local, regular number, like dealing a deck of playing cards)

        7) ppt-split (local, csplit with perl patterns, Perl Power Tools)
           https://github.com/briandfoy/PerlPowerTools/blob/master/bin/split 
           2016.08.02

        8) split_at_colchange (missing-text/local, add empty line when column changes)
        http://www1.cuni.cz/~obo/textutils/

        9) gate (local, Group And Transfer Entries, into files by field pattern)

        10) fsplit (local, split Fortran-77 into files by module name)

        12) mfs (local, split Fortran routines into files by name, handles "module")

        13) xsplit (local, explicit list of line numbers to files)

        14) nsplit (local, into "s" parts - bytes, not lines)

        15) bsplit (3rd/local, binary file with dd, originally named fsplit)

        16) f90split (3rd/local, split Fortran-90 source)

Briefly:

...
pl " Results, csplit, second file:"
csplit --quiet --prefix=f -z $FILE '/^[A-Z]/' '{*}'
head f01

pl " Results, ppt-split, first file:"
ppt-split -p '^[A-Z]' $FILE
head x.aab

produces:

-----
 Results, csplit, second file:
DROP
matt
sam

-----
 Results, ppt-split, first file:
ADD
john
mickey

Best wishes ... cheers, drl

Assuming headers are always uppercase:-

LC_ALL=C awk '/[A-Z]/{F="file."$1;next}NF{print > F}' file1

or

LC_ALL=C awk '/[A-Z]/{F="F"++i;next}NF{print > F}' file

Hello Scrutinizer,

Not sure if that could be really a case with OP's Input_file but in case OP's Input_file has few lines before either ADD or DROP strings then value of variable f will not be set and it may give following error.

awk '$1=="ADD"{f="F1"; next} $1=="DROP"{f="F2"; next} NF{print >f}'   Input_file
awk: (FILENAME=Input_file FNR=1) fatal: expression for `>' redirection has null string value

Where I have used Input_file as follows.

cfcgshg
hjgftyv
ADD
john
mickey

DROP
matt
sam

So I think we could add a small check condition for variable's value existance to as follows for safer side.

awk '$1=="ADD"{f="F1"; next} $1=="DROP"{f="F2"; next} NF && f{print >f}'   Input_file

Hello Diddy,

Could you please try following too and let me know if this helps you.

awk '{f=$1=="ADD"?"F1":($1=="DROP"?"F2":f)} NF && f && $1 != "DROP" && $1 != "ADD"{print > f}'   Input_file

Thanks,
R. Singh

or BEGIN {f = "F0"}