AWK split for multiple delimiters lines AWK split for multiple delimiters lines shell shell

AWK split for multiple delimiters lines


You can try something like this:

awk '/REC_DELIMITER\.TOP/ {    a=1    b=0    file = sprintf (FILENAME".split.%03d",++n)}    /REC_DELIMITER\.HIGH/ {    b=1    a=0    file = sprintf (FILENAME".split.%03d",++n)}  a {    print $0 > file}    b {    print $0 > file}' file


You need something like this (untested):

awk -v dtype="TOP" 'BEGIN { dbase = "^REC_DELIMITER\\."; delim = dbase dtype "$" }$0 ~ dbase { inBlock=0 }$0 ~ delim { inBlock=1; idx++ }inBlock { print > sprintf("original_file.split.%03d", idx) }' original_file


awk -vRS=REC_DELIMITER '/^.TOP\n/{print RS $0 > sprintf("original_file.split.%03d",n)};!++n' original_file

(Give or take an extra newline at the end.)

Generally, when input is supposed to be treated as a series of multi-line records with a special line as delimiter, the most direct approach is to set RS (and often ORS) to that delimiter.

Normally you'd want to add newlines to its beginning and/or end, but this case is a little special so it's easier without them.

Edited to add: You need GNU Awk for this. Standard Awk considers only the first character of RS.