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

前端 未结 4 431
温柔的废话
温柔的废话 2021-01-11 14:49

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

相关标签:
4条回答
  • 2021-01-11 15:08

    Given the awk script

    BEGIN { fout="headers" }
    /^$/ { fout="body" }
    { print $0 > fout }
    

    awk -f foo.awk < httpfile will write out the two files headers and body for you.

    0 讨论(0)
  • 2021-01-11 15:15
    $ 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.

    0 讨论(0)
  • 2021-01-11 15:18

    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
    
    0 讨论(0)
  • 2021-01-11 15:20

    You can use csplit:

    echo "a
    b
    c
    
    d
    e
    f" | csplit -s - '/^$/'
    

    Or

    csplit -s filename '/^$/'
    

    (assuming the contents of "filename" are the same as the output of the echo) would create, in this case, two files named "xx00" and "xx01". The prefix can be changed from "xx" to "outfile", for example, with -f outfile and the number of digits in the filename could be changed to 3 with -n 3. You can use a more complex regex if you need to deal with Macintosh line endings.

    To split a file at each empty line, you can use:

    csplit -s filename '/^$/' '{*}'
    

    The pattern '{*}' causes the preceding pattern to be repeated as many times as possible.

    0 讨论(0)
提交回复
热议问题