要在与regexp匹配的每一行上方添加一个空行,可以使用:
sed '/regexp/{x;p;x;}'
但是我想添加一个空行,不是上面的一行而是上面的两行匹配我的regexp。
我要匹配的模式是地址行中的邮政编码。
以下是文本格式的片段:
random info (belongs to previous business)
business name
business address
例如:
Languages Spoken: English
Arnold's Cove, Nfld (sub To Clarenville)
Nile Road, Arnolds Cove, NL, A0B1N0
我想在公司名称上方加一行:
Languages Spoken: English
Arnold's Cove, Nfld (sub To Clarenville)
Nile Road, Arnolds Cove, NL, A0B1N0
更具可读性的Perl,并能安全地处理多个文件。
有点像你在sed中的原始方法:
基本思想是打印延迟一行的所有内容(x更改保持和模式空间-打印是隐式的)。这需要完成,因为在我们检查下一行是否与regexp匹配之前,我们不知道是否要插入换行符。
($H这只是最后一行打印的技巧。它将最后一行追加到保持缓冲区中,以便最终的隐式打印命令也输出它。)
简单:
描述一下
编辑:稍微简单一点的版本。
相关问题 更多 >
编程相关推荐