use*_*830 2 linux bash shell command sed
我想在两行特定行之后将文本行插入另一个文本文件中.
在类似的东西之后插入
some text...
  example text
    (
    );
some text...
我有一个文本文件(包含两行文本),我想在括号之间插入.
如果我尝试插入的文本文件包含类似于以下内容的内容
need this;
in between the parentheses;
然后我希望结果看起来像这样
some text...
  example text
    (
     need this;
     in between the parentheses;
    );
some text...
什么是最好的解决方案可以工作(不必是sed).
编辑澄清
在需要插入文本的部分之前还有其他开括号,例如
sometext...
sometext (sometext)....
sometext
  (
  );
  exampletext
    (
    );
sometext...
所以,我认为"exampletext"需要引用然后查找括号.此外,它可能需要完全搜索"exampletext",因为文档中还有其他行带有"exampletextsometext ..."
完成此操作后,需要将文件添加到原始文件中.
如果开放(本身就是一条线就可以了
sed -e '/^(/r fileToInsert' firstFile
因为/^(/找到了要插入的行("以开括号开头的行"),并且r意味着"读取文件的内容并在此处插入.
如果确定插入点所需的表达式必须更复杂,请在注释中详细说明.例如,"完全是一个开括号而不是别的"/^($/
编辑感谢您澄清要求.如果您需要在example text后跟a 后插入此文本(,则以下脚本应该可以正常工作.将它放在自己的文件中,并使其成为可执行文件(chmod 755 myScript),然后运行./myScript.
#!/bin/bash
sed '
/exampletext/ {
  N
  /(/ r multi2.txt
}' multi1.txt
说明:
/exampletext/ {     find a match of this text, then…
N                   go to the next line
/(/                 match open parenthesis
r multi2.txt        insert file 'multi2.txt' here
}'                  end of script
multi1.txt          name of input file
请注意,这会产生输出stdout.您可以将其指向新文件名 - 例如
./myScript > newFile.txt
我用以下输入文件(multi1.txt)测试了这个:
some text...
sometext...
sometext (sometext)....
  exampletext
  not the right string
    (
    );
sometext
  (
  );
  exampletext
    (
    );
sometext...
它给出了输出
some text...
sometext...
sometext (sometext)....
  exampletext
  not the right string
    (
    );
sometext
  (
  );
  exampletext
    (
insert this
and that
    );
sometext...
我认为你想要的是什么?文本插入example text后面是一个左括号 - 但是当它们之间还有另一行时...
| 归档时间: | 
 | 
| 查看次数: | 2337 次 | 
| 最近记录: |