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.
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.
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.
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
$ 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With