Here is an easy-to-read solution with Bash
and grep
:
#!/bin/bash
while read line ; do
if FILE=$(grep -P -o '[a-z]*\.txt(?= - Starting)' <<< "$line"); then
F="$FILE"
fi
if ! grep '\*\*\*\*' <<< "$line" ; then
echo "$line" >> "$F"
fi
done
It gives the following result
$ cat file.txt
****************** abc.txt - Starting point ********************
abc
asd
asad
asfkn
sadjn
****************** abc.txt - End point ********************
****************** def.txt - Starting point ********************
fjhdsf
sdjnbs
askd
akdsn
dhskn
****************** def.txt - End point ********************
****************** xyz.txt - Starting point ********************
akdsnaskd
sakdnbasd
akjsndb
askjdb
akdsnb
****************** xyz.txt - End point ********************
$ cat file.txt | ./go.sh
****************** abc.txt - Starting point ********************
****************** abc.txt - End point ********************
****************** def.txt - Starting point ********************
****************** def.txt - End point ********************
****************** xyz.txt - Starting point ********************
****************** xyz.txt - End point ********************
$ cat abc.txt
abc
asd
asad
asfkn
sadjn
$ cat def.txt
fjhdsf
sdjnbs
askd
akdsn
dhskn
$ cat xyz.txt
akdsnaskd
sakdnbasd
akjsndb
askjdb
akdsnb
solved Extract data from txt file and create new file based on specific text dynamically