【发布时间】:2013-09-10 16:36:58
【问题描述】:
我想操作一个文本文件,以便将缩进的行块用 {{{ 和 }}} 括起来。
这是我卡住的地方:
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
如果有人花时间为我指明正确的方向,我将不胜感激。谢谢,
【问题讨论】:
-
+1 提出有趣问题的好问题。 .....但是除非您“嫁给” sed,否则 awk 将是解决此问题的一个更容易的方法。祝你好运
-
有趣!好的练习。让我们试试吧!