Create new file when three asterisks are encountered.

Hi All,

I have a text file which is currently formatted like this:

TEXT1

***

TEXT2

***

TEXT3

***

I want text before *** to go into separate files. For example,
1.dat

TEXT1

2.dat

TEXT2

3.dat

TEXT3

This is what I am doing but my code does not separate texts, rather it again places the entire text from the original file in another file 1.dat

awk 'BEGIN{RS="";FS="***"} {c++;f=c".dat"; printf("%s\n",$0) >> f}' file.txt

I also tried this but does not work as desired:

awk '/***/{close(c++".dat")}{print > c".dat"}' file.txt

I am using Linux with bash.

awk     'BEGIN{fname=++nr".txt"}
         {print $0 > fname}
         /\*\*\*/{fname=++nr".txt"}
        ' file.txt
1 Like

hmmm u can try something like..

 
awk '{if($0!~/\*\*\*/){var=var"\n"$0}else{n++;print var>n".dat";var=""}}' filename
1 Like

That will include the *** file delimiters in the output, which is not indicated by the sample data.

Nitpicks, but I'll point them out anyway. The regular expression will match any line that contains three consecutive asterisks and not just that sequence. Not knowing anything about the text in the file, it would be safer to anchor that regular expression on both ends.

If the file has many *** delimiters, the open file descriptor count could hit the user limit.

An alternative:

awk '$0=="***" || NR==1 {close(f); f=++n".dat"}  $0!="***" {print > f}' file

Regards,
Alister

1 Like

Another one:

awk '$0=="***"{f=++n".dat";print t > f;close(f);t="";b=0;next}{t=(!b?$0:t RS $0);if(!b) b=1}' file

And this will generate the files as you requested (1 file for the data before the 3 asterisk symbols in a line) and will not generate the fourth one as generated by some other solutions ;).

1 Like

I don't believe any of the solutions provided in posts #2 through #4 inclusive generate an extra empty file.

As I was responding, another approach occurred to me. For awk's which support a regexp RS:

awk '{f=NR".dat"; print > f; close(f)}' RS='\n\*\*\*\n' file

Regards,
Alister

1 Like