As part of a load test, I've a file with a generated list of requests. The format is below, in that, the payload is in between two newline characters; the GET indicating the start of a new request. I would like to extract only the payload.
Connection: false
Content-Length: 25
foo=bar1&foo2=bar2
GET http://localhost:1080/dasistgut
I have tried several combinations without success:
grep -zoP "(?<=(\n)).*(?<=(\GET))" sample.txt
grep -zoP "(?<=(\n)).*(?<=(\n))" sample.txt
grep -zoP "(?<=(\n)).*(?<=\n)" sample.txt
Expected output: foo=bar1&foo2=bar2
You may use this gnu grep
:
var=$(grep -zoP '.+(?=\R{2}GET )' file)
echo "$var"
foo=bar1&foo2=bar2
Lookahead assertion (?=\\R{2}GET )
makes sure that input has exact 2 newlines ( \\R
) followed by "GET "
ahead of current position.
Alternative awk
based solution:
awk '!NF{++n; next} n && /^GET /{print p} {p=$0; n=0}' file
foo=bar1&foo2=bar2
使用awk
匹配空白行,获取下一行并打印,然后跳过下一行(应始终为空白)。
awk '/^$/ { getline; print; getline; }' sample.text
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.