To Replace Curl-i Header and Body in a Shell Script

Asked 2 months ago, Updated 2 months ago, 3 views

curl-i seems to yield the following output:

status code
header
header
header

body
body
body

I would like to use this first \n\n as a boundary and eventually get the output similar to the following:
What should I do?

body
body
body

status code
header
header
header

If possible, I would appreciate it if you could use one liner (like connecting it with a pipe).

shellscript shell

2022-09-30 13:52

2 Answers

The following is an example of how to use awk, where RS(Record Separator) is set to the HTTP response header and the response body delimiter string (\r\n\r\n).

$curl-s-i https://example.com/ |awk-vRS='\r\n\r\n' 'NR==1 {first=$0}NR>1;END {printfirst}'
<!doctype html>
<html>
<head>
    <title>Example Domain</title>
                :

</body>
</html>

HTTP/2200 
accept-ranges:bytes
age: 411753
cache-control —max-age=604800
content-type —text/html; charset=UTF-8
date:Tue, 11 Aug 2020 10:22:39 GMT
etag: "3147526947+ident"
expires:Tue, 18 Aug 2020 10:22:39 GMT
last-modified: Thu, 17 Oct 2019 07:18:26 GMT
server —ECS(sjc/4E74)
vary —Accept-Encoding
x-cache:HIT
content-length —1256


2022-09-30 13:52

Anyway, I wrote it down.Maybe I can write better.

$cathoge.txt
status code
header
header
header

body
body
body
$ cathoge.txt | awk'/^$/{f=1; next}; { if(!f){d=d$0"\n"} else {print}}};END {printf"\n"d}'
body
body
body

status code
header
header
header

I also wrote it on sed.

$cathoge.txt | sed-e'1, /^$/{/^$/d;H;d}'-e'$G'
body
body
body

status code
header
header
header


2022-09-30 13:52

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.