Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split file on first empty line in a portable way in shell (e.g. using sed)?

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.

like image 984
Jakub Narębski Avatar asked Oct 29 '09 15:10

Jakub Narębski


People also ask

How do you split a file by line number in Unix?

If you want your file to be split based on the number of lines in each chunk rather than the number of bytes, you can use the -l (lines) option. In this example, each file will have 1,000 lines except, of course, for the last one which may have fewer lines.

How do you split an empty line in Python?

To split text by empty line, split the string on two newline characters, e.g. my_str. split('\n\n') for POSIX encoded files and my_str. split('\r\n\r\n') for Windows encoded files.


2 Answers

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
like image 78
mouviciel Avatar answered Oct 02 '22 08:10

mouviciel


$ 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.

like image 39
John Kugelman Avatar answered Oct 02 '22 09:10

John Kugelman