sed — 加入一系列选定的行 [英] sed — joining a range of selected lines

查看:45
本文介绍了sed — 加入一系列选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 sed 的初学者.我知道可以像这样将命令(或一组命令)应用到特定范围的行

I'm a beginner to sed. I know that it's possible to apply a command (or a set of commands) to a certain range of lines like so

sed '/[begin]/,/[end]/ [some command]'

其中 [begin] 是指定范围开始行的正则表达式,[end] 是指定范围结束行的正则表达式 (但包含在范围内).

where [begin] is a regular expression that designates the beginning line of the range and [end] is a regular expression that designates the ending line of the range (but is included in the range).

我正在尝试使用它来指定文件中的一系列行并将它们全部合并为一行.这是我最好的尝试,但没有奏效:

I'm trying to use this to specify a range of lines in a file and join them all into one line. Here's my best try, which didn't work:

sed '/[begin]/,/[end]/ {
N
s/\n//
}
'

我可以毫无问题地选择我想要的一组行,但我似乎无法将它们全部合并为一行.如果有人能指出我正确的方向,我将不胜感激.

I'm able to select the set of lines I want without any problem, but I just can't seem to merge them all into one line. If anyone could point me in the right direction, I would be really grateful.

推荐答案

使用 GNU sed 的一种方式:

sed -n '/begin/,/end/ { H;g; s/^\n//; /end/s/\n/ /gp }' file.txt

这篇关于sed — 加入一系列选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆