I have a file with below data
.domain bag
.set bag1
bag1
abc1
.set bag2
bag2
abc2
.domain cat
.set bag1:cat
bag1:cat
abc1:cat
.set bag2:cat
bag2:cat
abc2:cat
I want to split this file into two (bag1.txt and bag2.txt) based on the set value.
bag1.txt should look like :
.domain bag
.set bag1
bag1
abc1
.domain cat
.set bag1:cat
bag1:cat
abc1:cat
bag2.txt should look like :
.domain bag
.set bag2
bag2
abc2
.domain cat
.set bag2:cat
bag2:cat
abc2:cat
the .domain line is common for both the files.
I tried the command below but it is not working.
nawk '{if($0~/.set/){split($2,a,":");filename=a[1]".text"}if(filename=".text"){print|"tee *.text"}else{print >filename}}' file.txt