根据下一行的格式删除换行符 [英] Remove newline depending on the format of the next line
问题描述
我有一个这种格式的特殊文件:
I have a special file with this kind of format :
title1
_1 texthere
title2
_2 texthere
我希望所有以_"开头的换行符都作为第二列放在之前的行
I would like all newlines starting with "_" to be placed as a second column to the line before
我尝试使用带有此命令的 sed 来做到这一点:
I tried to do that using sed with this command :
sed 's/_\n/ /g' filename
但它没有给我我想做的事情(基本上什么都不做)
but it is not giving me what I want to do (doing nothing basically)
谁能指出我正确的做法吗?
Can anyone point me to the right way of doing it ?
谢谢
推荐答案
尝试以下解决方案:
在 sed 中,循环完成,创建了一个标签 (:a
),当不匹配最后一行 ($!
) 时追加下一行 (N
) 并返回标签 一个
:
In sed the loop is done creating a label (:a
), and while not match last line ($!
) append next one (N
) and return to label a
:
:a
$! {
N
b a
}
在此之后,我们将整个文件存入内存,因此对每个以换行符开头的_
进行全局替换:
After this we have the whole file into memory, so do a global substitution for each _
preceded by a newline:
s/\n_/ _/g
p
总而言之:
sed -ne ':a ; $! { N ; ba }; s/\n_/ _/g ; p' infile
结果:
title1 _1 texthere
title2 _2 texthere
这篇关于根据下一行的格式删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!