views:

334

answers:

4

I want to split a file containg HTTP response into two files: one containing only HTTP headers, and one containg the body of a message. For this I need to split a file into two on first empty line (or for UNIX tools on first line containing only CR = '\r' character) using a shell script.

How to do this in a portable way (for example using sed, but without GNU extensions)? One can assume that empty line would not be first line in a file. Empty line can got to either, none or both of files; it doesn't matter to me.

+5  A: 
$ cat test.txt
a
b
c

d
e
f
$ sed '/^$/q' test.txt 
a
b
c

$ sed '1,/^$/d' test.txt 
d
e
f

Change the /^$/ to /^\s*$/ if you expect there may be whitespace on the blank line.

John Kugelman
It should probably be `/^\r$/`(or just in case `/^\r?$/`)
Jakub Narębski
+3  A: 

Given the awk script

BEGIN { fout="headers" }
/^$/ { fout="body" }
{ print $0 > fout }

awk -f foo.awk < httpfile will write out the two files headers and body for you.

jamessan
+1 elegant (to 15 chars)
Ewan Todd
A: 

You can extract the first part of your file (HTTP headers) with:

awk '{if($0=="")exit;print}' myFile

and the second part (HTTP body) with:

awk '{if(body)print;if($0=="")body=1}' myFile
mouviciel
no need to cat to awk. awk takes in a file as input
ghostdog74
You are right, I have edited my answer.
mouviciel
+2  A: 

You can use csplit:

echo "a
b
c

d
e
f" | csplit -s - '/^$/'

Or

csplit -s filename '/^$/'

(assuming the contents of "filename" are the same as the output of the echo) would create, in this case, two files named "xx00" and "xx01". The prefix can be changed from "xx" to "outfile", for example, with -f outfile and the number of digits could be changed to 3 with -n 3. You can use a more complex regex if you need to deal with Macintosh line endings.

Dennis Williamson
+1 you can also use it to split the file into more than 2 parts.
Zac Thompson