Cut a file with tags

Hi,

I'm looking for a way to cut a file which has the same tag in it.

Example:

TAG
AAAA
BBBB
CCCCCC
DDDD
TAG
EE
FFF
GGGG
TAG
HHHHHH
TAG
IIII
JJ

I would like to take everything between the tags and send the result in a file like that:
File1

AAAA
BBBB
CCCCCC
DDDD

File2

EE
FFF
GGGG

File3

HHHHHH

Etc...
I guess a awk command exist to do this operation, but I have no idea how to do it....

Some help please? :slight_smile:

Try

awk '/TAG/{a++;next} {print > "File"a}' file
1 Like
awk '/TAG/ {if(file>""){close{file}; cnt++; file=sprintf("%s%d", "file", cnt);next}
                  {print $0 > file} ' inputfile > outputfile
       

Thanks pamu!!!
That works very well!

jim mcnamara, I didn't try but thank you!

Hello,

maybe with command csplit

Regards.