Hi jethro,
Using the script you provided:
awk 'NR>1 {print > FILENAME"."(NR-1)}' RS="--dump[^\n]*" file
on Mac OS X I get:
awk: syntax error at source line 1
context is
NR>1 {print > >>> FILENAME"." <<<
awk: illegal statement at source line 1
awk: illegal statement at source line 1
The standards explicitly say that it is unspecified whether:
print > FILENAME"."(NR-1)
is interpreted as:
(print > FILENAME)"."(NR-1)
(as it is in awk
on OS X) or as:
print > (FILENAME"."(NR-1))
(as it is on your system).
Changing your code to:
awk 'NR>1 {print > (FILENAME"."(NR-1))}' RS="--dump[^\n]*" file
with a file named file
containing:
--dump-4564564.some.jibberish.whatever
From: user@domain.com
To: myfriend@email.com
Email Body
Best Regards,
Email Author
--dump-789789863.random.numbers.maybe
From: anotheruser@domain.com
To: someguy@planet.earth
Email Body
Your Friend,
another user
I get 6 files as shown here:
-rw-r--r-- 1 dwc staff 1 Apr 25 20:32 file.1
-rw-r--r-- 1 dwc staff 5 Apr 25 20:32 file.2
-rw-r--r-- 1 dwc staff 119 Apr 25 20:32 file.3
-rw-r--r-- 1 dwc staff 1 Apr 25 20:32 file.4
-rw-r--r-- 1 dwc staff 5 Apr 25 20:32 file.5
-rw-r--r-- 1 dwc staff 126 Apr 25 20:32 file.6
where file.1
and file.4
contain only a <newline> character; file.2
and file.5
contain "dump" and the line terminating <newline> character; and file.3
and file.6
contain the requested mail messages plus the tail end of the headers:
file.3
:
4564564.some.jibberish.whatever
From: user@domain.com
To: myfriend@email.com
Email Body
Best Regards,
Email Author
file.6
:
789789863.random.numbers.maybe
From: anotheruser@domain.com
To: someguy@planet.earth
Email Body
Your Friend,
another user
Note that there are two empty lines at the end of both of the above files. I believe that only one empty line was expected.
The number of file descriptors used by awk
for open input and output streams is implementation defined. Some versions of awk
used to limit you to 9 open files. Most systems today allow around 1024 or 2048 file descriptors per process and (unless you have privileges to up that limit before you invoke awk), awk won't be able to have more files open than the number of file descriptors available to it. You may have noticed that my script closed the previous output file before opening the next output file. This is usually a much better practice unless you know that your script will open less than ten files in its lifetime.
---------------
OOPS. I originally said that file.2 and file.5 contained "code". That has been corrected above. They contain "dump"; not "code".