V
V
Vitaliy2019-11-06 22:53:34
linux
Vitaliy, 2019-11-06 22:53:34

How to select the required number of lines from a file?

There is such a file

HTTP/2 301 
location: https://www.google.com/
content-type: text/html; charset=UTF-8
cache-control: public, max-age=2592000
server: gws
content-length: 220
x-xss-protection: 0
x-frame-options: SAMEORIGIN
alt-svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000

HTTP/2 301 
location: https://www.google.com/
content-type: text/html; charset=UTF-8
cache-control: public, max-age=2592000
server: gws
content-length: 220
x-xss-protection: 0
x-frame-options: SAMEORIGIN
alt-svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000

I need to separate the last response into a separate file. But the problem is that I do not know how many lines the answer will take, but I know that it will be separated from the previous one by a new line (as in the example).
How to do this in BASH?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
J
jcmvbkbc, 2019-11-07
@jcmvbkbc

tac file | sed -n '1,/^$/{/./p}' | tac
The first tac reads the file backwards, sed cuts out the first block of non-blank lines, the second tac reverses that block to its original state.

Q
q2zoff, 2019-11-07
@q27off

Printing the last reply:
Printing all responses starting from the second one (relevant for your example):
sed -n ' 0,/^$/!p' input.txt

A
Adamos, 2019-11-06
@Adamos

1. Set the variable FILE_N - the sequence number of the output file
2. Read this source file line by line.
3. Compare what you read with the ^HTTP/2 mask
4. If it matches, increase FILE_N
5. Send the read line to a file whose name is formed from FILE_N
6. End of the loop started in step 2

S
Saboteur, 2019-11-07
@saboteur_kiev

I suppose that it is better to focus on the title, and not on an empty line:
tac file | sed -n '1,/^HTTP\/2/p' | tac

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question