sed:多行的有条件合并 [英] sed: conditional merge of multiple lines
本文介绍了sed:多行的有条件合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要基于模式使用sed合并文件的行.例如:
I need to merge line of a file using sed based on a pattern. eg:
输入文件:
X1 A B C D E F
\+ G H I J 1
\+ LK T PP E OO 2
X2 DDF F Y
\+ J W Q
....
预期输出:
X1 A B C D E F G H I J 1 LK T PP E OO 2
X2 DDF F Y J W Q
..
我想在vi编辑器(:%s/\ n +//g)中可以等同于wat
I would like to equivalent of wat is possible in vi editor (:%s/\n+/ /g)
在网上搜索时,我找到了一个解决方案,从逻辑上讲应该是可行的
Searching the web I found a solution, which logically should have worked
sed -e'{:a;N;s/\ n +//g;ta}'infile
sed -e '{:a; N; s/\n+/ /g; ta}' infile
但是此命令违背了我的理解和逻辑,并产生了输出
But this command defies my understanding and logic, and has produced output
X1 A B C D E F
\+ G H I J 1 LK T PP E OO 2
X2 DDF F Y
\+ J W Q
....
欢迎任何想法,并且提前致谢
Any ideas are welcome, & Thanks in advance
Srisurya
推荐答案
这可能对您有用:
sed ':a;$!N;s/\n+//;ta;P;D' file
说明:
-
:a
是循环占位符 -
$!N
表示是否将文件末尾附加到当前行. -
s/\ n +//
表示删除行尾的加号 -
ta
表示最后一次替换是否有效,分支到a
循环占位符 -
P
打印到第一行. -
D
删除直到并包括第一条换行符.
:a
is a loop placeholder$!N
means if not end-of-file append next line to current line.s/\n+//
means delete an end on line followed by a plus signta
means if last substitution worked branch toa
loop placeholderP
print upto the first newline.D
delete upto and including the first newline.
这篇关于sed:多行的有条件合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文