如何在 shell 中以可移植的方式在第一个空行上分割文件(例如使用 sed)?

发布于 2024-08-09 12:40:07 字数 274 浏览 7 评论 0原文

我想将包含 HTTP 响应的文件拆分为两个文件:一个仅包含 HTTP 标头,另一个包含消息正文。为此,我需要使用 shell 脚本 在第一个空行上将文件分成两个(或者对于第一行仅包含 CR = '\r' 字符的 UNIX 工具) 。

如何以可移植的方式执行此操作(例如使用 sed,但不使用 GNU 扩展)?人们可以假设空行不会是文件中的第一行。空行可以到达其中一个文件,也可以不到达或同时到达两个文件;这对我来说并不重要。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彼岸花ソ最美的依靠 2024-08-16 12:40:07

您可以使用 csplit:

echo "a
b
c

d
e
f" | csplit -s - '/^$/'

或者

csplit -s filename '/^$/'

(假设“filename”的内容与 echo 的输出相同)在本例中将创建两个名为“xx00”和“xx01”的文件。前缀可以从“xx”更改为“outfile”,例如使用-f outfile,文件名中的位数可以使用-n 3更改为3代码>.如果需要处理 Macintosh 行结尾,可以使用更复杂的正则表达式。

要在每个空行处分割文件,您可以使用:

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

模式 '{*}' 使前面的模式重复尽可能多的次数。

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.

眼睛会笑 2024-08-16 12:40:07
$ cat test.txt
a
b
c

d
e
f
$ sed '/^$/q' test.txt 
a
b
c

$ sed '1,/^$/d' test.txt 
d
e
f

如果您预计空行上可能有空格,请将 /^$/ 更改为 /^\s*$/

$ 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.

作业与我同在 2024-08-16 12:40:07

给定 awk 脚本

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

awk -f foo.awk < httpfile 将为您写出 headersbody 两个文件。

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.

不…忘初心 2024-08-16 12:40:07

您可以使用以下命令提取文件的第一部分(HTTP 标头):

awk '{if($0~"^\r*$")exit;print}' myFile

并使用以下命令提取第二部分(HTTP 正文):

awk '{if(body)print;if($0~"^\r*$")body=1}' myFile

You can extract the first part of your file (HTTP headers) with:

awk '{if($0~"^\r*
quot;)exit;print}' myFile

and the second part (HTTP body) with:

awk '{if(body)print;if($0~"^\r*
quot;)body=1}' myFile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文