追加到previous线匹配 [英] Append to the previous line for a match
本文介绍了追加到previous线匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 SED
或 AWK
如果发现匹配追加到previous行?
can I use sed
or awk
to append to the previous line if a match is found ?
我有一个具有格式的文件:
I have a file which has the format :
INT32
FSHL (const TP Buffer)
{
INT32
FSHL_lm (const TP Buffer)
{ WORD32 ugo = 0; ...
我所试图做的是扫描独立开括号 {
并追加到previous非空行.The比赛不应该为发生开括号在同一行的任何附加。
What I am trying to do is scan for independant open braces {
and append it to the previous non-blank line .The match should not occur for an open brace appended by anything in the same line .
预期的输出:
INT32
FSHL (const TP Buffer){
INT32
FSHL_lm (const TP Buffer)
{ WORD32 ugo = 0; ...
谢谢你的答复。
推荐答案
这可能会为你工作(GNU SED):
This might work for you (GNU sed):
sed '$!N;s/\n\s*{\s*$/{/;P;D' file
说明:
-
$ N!
,除非在最后一行追加下一行到模式空间。 -
S / \\ n \\ S * {\\ s * $ / {/
更换一个换行符后没有或后面的空格后面跟一个大括号的数量绝不或任何空白字符串的结尾量,由大括号。 -
P
打印高达且包括第一换行符。 -
D
删除高达和包括第一个换行符(如果这样不开始新的循环)。
$!N
unless the last line append the next line to the pattern space.s/\n\s*{\s*$/{/
replace a linefeed followed by no or any amount of white space followed by an opening curly brace followed by no or any amount of white space to the end of the string, by an opening curly brace.P
print upto and including the first newline.D
delete upto and including the first newline (if so do not start a new cycle).
这篇关于追加到previous线匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文