如何在模式前插入换行符? [英] How to insert a newline in front of a pattern?
本文介绍了如何在模式前插入换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在一行内的模式前插入换行符?
How to insert a newline before a pattern within a line?
例如,这将在正则表达式模式后面插入一个换行符.
For example, this will insert a newline behind the regex pattern.
sed 's/regex/&\n/g'
我怎样才能做同样的事情,但在模式前面?
How can I do the same but in front of the pattern?
鉴于此示例输入文件,要匹配的模式是电话号码.
Given this sample input file, the pattern to match on is the phone number.
some text (012)345-6789
应该变成
some text
(012)345-6789
推荐答案
其他一些答案不适用于我的 sed 版本.切换 &
和 \n
的位置确实有效.
Some of the other answers didn't work for my version of sed.
Switching the position of &
and \n
did work.
sed 's/regexp/\n&/g'
这似乎不适用于 OS X,除非您安装 gnu-sed
.
This doesn't seem to work on OS X, unless you install gnu-sed
.
这篇关于如何在模式前插入换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文