Create new file when three asterisks are encountered.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create new file when three asterisks are encountered.
# 1  
Old 09-12-2012
Create new file when three asterisks are encountered.

Hi All,

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

Code:
TEXT1

***

TEXT2

***

TEXT3

***

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

2.dat
Code:
TEXT2

3.dat
Code:
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

Code:
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:

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

I am using Linux with bash.
# 2  
Old 09-12-2012
Code:
awk     'BEGIN{fname=++nr".txt"}
         {print $0 > fname}
         /\*\*\*/{fname=++nr".txt"}
        ' file.txt

This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-12-2012
hmmm u can try something like..

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

This User Gave Thanks to vidyadhar85 For This Post:
# 4  
Old 09-12-2012
Quote:
Originally Posted by RudiC
Code:
awk     'BEGIN{fname=++nr".txt"}
         {print $0 > fname}
         /\*\*\*/{fname=++nr".txt"}
        ' file.txt

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


Quote:
Originally Posted by vidyadhar85
hmmm u can try something like..
Code:
 
awk '{if($0!~/\*\*\*/){var=var"\n"$0}else{n++;print var>n".dat";var=""}}' filename

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:
Code:
awk '$0=="***" || NR==1 {close(f); f=++n".dat"}  $0!="***" {print > f}' file

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 09-12-2012
Another one:
Code:
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 Smilie.

Last edited by elixir_sinari; 09-12-2012 at 11:21 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 09-12-2012
Quote:
Originally Posted by elixir_sinari
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 Smilie.
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:
Code:
awk '{f=NR".dat"; print > f; close(f)}' RS='\n\*\*\*\n' file

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing multiple asterisks in vi

i need to replace all occurrences of "period asterisk" as it is shown in this: blah blah .*:.*:.* blah blah with: :: so that the end result looks like this: blah blah :: blah blah I tried different variations of the following but it didint work: %s_ .*:.*:.* _ :: _g (2 Replies)
Discussion started by: SkySmart
2 Replies

2. UNIX for Advanced & Expert Users

Show Asterisks when changing Password

Note: **Showing Asterisks when using SUDO is not what I am looking for. That method is well documented** Short Description: We have a requirement where users want to see that they are typing a password when logging into a RedHat box or when they are changing their password -- instead of... (1 Reply)
Discussion started by: caperjm
1 Replies

3. Shell Programming and Scripting

Eliminate or ignore asterisks in data when parsing

I have data file that has this in it: data.txt ......... ......... PPJ97**2017PPJ97**2017-03-21-13.35.15.887208********************START ERROR LOGGING****************** PPJ97**2017-03-21-13.35.15.887208** PROMPT APPLICATION ERROR ** PPJ97**2017-03-21-13.35.15.887208** IN TIMESTAMP |... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. Shell Programming and Scripting

build a string of asterisks elegantly

hi in a script i hate string definitions like str="***********************************" who can help to build a 50 character long string of asterisks more elegantly? any hint is welcome thanks and regards lazy (18 Replies)
Discussion started by: lazybaer
18 Replies

5. Shell Programming and Scripting

need to replace asterisks

I need to replace occurrences of twelve asterisks "************" with the string " 0000000.00" . Note that there are two spaces in front of the first zero. How can I do this using awk or sed? (3 Replies)
Discussion started by: mustang_9333
3 Replies

6. Shell Programming and Scripting

Assistence With Using Asterisks in GREP Expressions

I am attempting to find all complete words which contain an asterisk at the beginning and the end - for instance, "*Hello?*" or "*you*". From what I've read, I would have thought that the following expression would do that just fine: \<\*.*\*\> \< denoting the beginning of a word. \*... (12 Replies)
Discussion started by: MagusScythe
12 Replies

7. Shell Programming and Scripting

Double asterisks

When I go$ echo *I get a directory listing. When I go$ echo * *I get a directory listing, followed by a second identical directory listing. When I go$ echo **I only get one directory listing. What happens to the second asterisk in this case? Why doesn't it expand? I haven't been able to sleep... (2 Replies)
Discussion started by: na5m
2 Replies

8. UNIX Benchmarks

Encountered error!

I used this on an AIX machine and encountered the following error. $ ls -l total 600 -rwxrwxrwx 1 e26936 dba 1491 Feb 07 1992 MANIFEST -rwxrwxrwx 1 e26936 dba 8148 Apr 05 1992 Makefile -rwxrwxrwx 1 e26936 dba 4852 Sep 06 2003 README -rwxrwxrwx... (0 Replies)
Discussion started by: puspendu
0 Replies

9. UNIX for Advanced & Expert Users

554 Unallowed chars encountered

My Exchange v5.5 IMS server received an inbound internet-based message that it could not processed. The message header appeared to be corrupt and had a line that read, "Diagnostic-Code: smtp;554 Unallowed chars encountered." The message header also mentions UTF-7. All internet-based messages are... (1 Reply)
Discussion started by: abibbens
1 Replies
Login or Register to Ask a Question