How to Split Text Between Separator into Multiple Files

bashsplittext processing

I have a text file contaning the following:

"random
textA"
"random
random
textB"

The separator is "

How can I split the containt into multiple file as follow using a bash command?

File 1 :

random
textA

File 2 :

random
random
textB

I came into examples using csplit or awk but they does not cover this text layout.

Best Answer

Simple awk command:

awk 'NR%2==0{ print > "File "++i }' RS='"' file

RS defines " as record separator and NR is the record number. If the record number was modulo of 2 (because we have another first " for records), then print the current record $0 into a File #.

Related Question