Regex sed插入/追加和打印图案空间
我想操纵一个文本文件,这样缩进的行块将被{{{{和}}}包围 这就是我被困的地方:Regex sed插入/追加和打印图案空间,regex,awk,sed,Regex,Awk,Sed,我想操纵一个文本文件,这样缩进的行块将被{{{{和}}}包围 这就是我被困的地方: 1 /^\ [^\ ]/,/^[^\ ]/{ # match range: all indented plus line after that 2 b fixIndented # branch 3 } 4 5 /^[^\ ]/{p;b} # print all non-indented outside range and
1 /^\ [^\ ]/,/^[^\ ]/{ # match range: all indented plus line after that
2 b fixIndented # branch
3 }
4
5 /^[^\ ]/{p;b} # print all non-indented outside range and exit.
6
7 :fixIndented
8 /^[^\ ]/{ # match last line of range
9 x; # swap Holdbuffer und patternSpace, edit patternSpace
10 i\
11 {{{
12 a\
13 }}}
14 p;
15 x;p;
16 }
17 H; # write each line in range into holdBuffer
我认为第15行应该读入保持缓冲区(包含我在第9行中交换的内容),然后在打印了被操纵的图案空间(第10-13行)之后打印它(14)。
但事实并非如此。相反,它似乎将保留缓冲区中的行合并到模式空间中。像这样:
bla
blubb
foo1
bla2
foo3
sadgfasdf
bar
foo
变成:
bla
blubb
{{{
foo1
bla2
foo3
sadgfasdf
}}}
bar
foo
如果有人能抽出时间给我指出正确的方向,我将不胜感激。谢谢,sed也可以,但awk非常简单:
cat file
bla
blubb
foo1
bla2
foo3
sadgfasdf
bar
foo
awk '!s && /^ /{s=1; $0 = " {{{" ORS $0} s && /^[^ ]/{s=0; $0 = " }}}" ORS $0}1' file
bla
blubb
{{{
foo1
bla2
foo3
}}}
sadgfasdf
bar
foo
+1对于好问题和有趣问题。。。。。但除非你“嫁给”了sed,否则awk将是解决这个问题的更容易的方法。祝你好运!好的运动。让我们试试@迈克尔:我告诉过你会更容易;-)祝大家好运。这就解决了问题,非常感谢!明天在工作中,我将不得不检查它是如何与更大的环境相融合的(也就是说,看看缩进的大括号是否会成为一个问题),但它是一个令人印象深刻的一行,做了我想要的@谢尔特:不,我没有和塞德结婚,看到这件事用awk解决是多么容易,我肯定会考虑现在就离婚:)