I have a file that consists of tags and content descriptions, e.g.:
@ABC-1111 @ANYTAG
Content: description
content1
content2
@ABC-2222 @ABC-0000 @ANYTAG
Content: another description
content1
content2
@ANYTAG @ABC-1111 @ABC-0000
Content: yet another description
content1
content2
@ABC-0000
Content: anything here
content1
content2
I would like to split this file based on the tags it contains with a certain prefix (e.g "ABC") along with its content below it. So from the example file above, it will be split into 3 files (since there are 3 tags with "ABC" prefix).
File "ABC-0000" (found 3 instances in the file):
@ABC-2222 @ABC-0000 @ANYTAG
Content: another description
content1
content2
@ANYTAG @ABC-1111 @ABC-0000
Content: yet another description
content1
content2
@ABC-0000
Content: anything here
content1
content2
File "ABC-1111" (found two instances in the file):
@ABC-1111 @ANYTAG
Content: description
content1
content2
@ANYTAG @ABC-1111 @ABC-0000
Content: yet another description
content1
content2
File "ABC-2222" (found 1 instance in the file):
@ABC-2222 @ABC-1010 @ANYTAG
Content: another description
content1
content2
I was trying to use bash script with sed:
for i in $(grep -Eo ‘@ABC-[0-9]+’ $file | sort -u); do
sed -n -r "/${i}/,/^\s*$/p" $file >> $i.out
done
seems it works only if there is a blank line between the content of the tag with the next tag line.
Is there a way to do this with grep, sed, or awk? Or maybe in python?
Thanks!!