在UNIX/Linux操作系统下,如何用sed替换换行(\ n)模式? [英] How to replace a pattern with newline (\n) with sed under UNIX / Linux operating systems?
本文介绍了在UNIX/Linux操作系统下,如何用sed替换换行(\ n)模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个txt文件,其中包含:
I have a txt file which contains:
Some random
text here. This file
has multiple lines. Should be one line.
我使用:
sed '{:q;N;s/\n/:sl:/g;t q}' file1.txt > singleline.txt
并获得:
Some random:sl:text here. This file:sl:has multiple lines. Should be one line.
现在我想用newline (\n)
字符替换:sl:
模式.当我使用时:
Now I want to replace the :sl:
pattern with newline (\n)
character. When I use:
sed 's/:sl:/&\n/g' singleline.txt
我得到:
Some random:sl:
text here. This file:sl:
has multiple lines. Should be one line.
如何用换行符替换模式,而不是在模式后添加换行符?
How to replace the pattern with newline character instead of adding newline character after the pattern?
推荐答案
Sed使用&
作为匹配模式的快捷方式.因此,您要用:s1:\n
替换:s1:
.
Sed uses &
as a shortcut for the matched pattern. So you are replacing :s1:
with :s1:\n
.
像这样更改您的sed命令:
Change your sed command like this:
sed 's/:sl:/\n/g' singleline.txt
这篇关于在UNIX/Linux操作系统下,如何用sed替换换行(\ n)模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文