2014-11-08 50 views
1

我需要在每行下方使用'vi'和/或'sed'编辑器插入一段文本。需要被插入各线下方的文本的块如下(表示一个空行):使用vi和/或sed编辑器插入每行下方的文本块

first line. 
second line. 
... 
n-th line. 
<empty line> 

插入之前,文本看起来像如下:

This is the first line. 
This is the second line. 
... 
This is the last line. 

插入后,将文字将如下所示:

This is the first line. 
    first line. 
    second line. 
    ... 
    n-th line. 
    <empty line> 
    This is the second line. 
    first line. 
    second line. 
    ... 
    n-th line. 
    <empty line> 
    ... 
    This is the last line. 
    first line. 
    second line. 
    ... 
    n-th line. 
    <empty line> 

有人吗?谢谢。

回答

0

假设你的块要插入的文本保存在一个名为block文件,你想这个过程被称为file文件:

GNU SED:

sed -r '/^/r block' file 
+0

这工作很好,除了我必须管输出到一个文件或添加** - 我**开关,即* sed -i -r'/ ^/r块'文件*,覆盖现有的文件。谢谢。 – Mazilo 2014-11-10 04:25:42

0

这里是一个awk版本。

awk '{$0=$0"\n first line.\n second line.\n ...\n n-th line.\n"}1' file 
This is the first line. 
first line. 
second line. 
... 
n-th line. 

This is the second line. 
first line. 
second line. 
... 
n-th line. 

... 
first line. 
second line. 
... 
n-th line. 

This is the last line. 
first line. 
second line. 
... 
n-th line. 
+0

我测试了你的建议,它的工作原理与前面描述的一样,除了输出需要定向到一个文件。谢谢。 – Mazilo 2014-11-10 04:35:18

+0

@ user3767809只需在命令后面加上'> newfile'以将其保存到文件中 – Jotne 2014-11-10 07:49:03

+0

是的,那就行了。谢谢。 – Mazilo 2014-11-11 12:48:14

0

那么,你可以做一个录音。如果块很小,您可以键入它,否则您可以从文件中读取。 对于例如,对于下面的文字,

 
This is the first line. 
This is the second line. 
... 
This is the last line. 
  1. 将光标置于第一行的开头,并键入QA启动名称的记录一个
  2. 与实际按照此键入文本的块:ø然后键入块
 
first line. 
second line. 
... 
n-th line. 

  • Esc键+ J(返回到命令模式,并移动到下一行)
  • q(停止拍摄)
  • 这一点后,将宏的美丽开始。 现在你在文件的原始第二行。类型@ a。您将在原始第二行下方添加文本块并将光标移动到原始第三行。
    或者,如果您知道原始文件有n行。你可以输入n-1 @ a。文本块将被添加到所有行的下方。

    相关问题