简体   繁体   English

sed命令在多行搜索后插入多行字符串

[英]sed command to insert multi line string after multi line search

I would like to insert lines of text into another text file after two specific lines. 我想在两行特定行之后将文本行插入另一个文本文件中。

Inserting after something similar to 在类似的东西之后插入

some text...
  example text
    (
    );
some text...

I have a text file(containing two lines of text) that I would like sed to insert between the parentheses. 我有一个文本文件(包含两行文本),我想在括号之间插入。

If the text file I am trying to insert contains something similar to the following 如果我尝试插入的文本文件包含类似于以下内容的内容

need this;
in between the parentheses;

Then I would like the result to look like this 然后我希望结果看起来像这样

some text...
  example text
    (
     need this;
     in between the parentheses;
    );
some text...

What ever the best solution would be can work(doesn't have to be sed). 什么是最好的解决方案可以工作(不必是sed)。

EDIT for clarification 编辑澄清

There are other open parentheses before the section where the text needs to be inserted eg 在需要插入文本的部分之前还有其他开括号,例如

sometext...
sometext (sometext)....
sometext
  (
  );
  exampletext
    (
    );
sometext...

So, I would think that the "exampletext" would need to be reference to then look for the parentheses. 所以,我认为“exampletext”需要引用然后查找括号。 Also, it would would probably need to search for "exampletext" exactly since there are other lines within the document with "exampletextsometext..." 此外,它可能需要完全搜索“exampletext”,因为文档中还有其他行带有“exampletextsometext ...”

Once this has been completed the addition to the file would need to be saved over the original file. 完成此操作后,需要将文件添加到原始文件中。

If the open ( is on a line by itself you could just do 如果打开(单独就行就可以了

sed -e '/^(/r fileToInsert' firstFile

since the /^(/ finds the line where you want to insert ("a line starting with an open parenthesis"), and r means "read contents of file and insert at this point. 因为/^(/找到要插入的行(“以开括号开头的行”), r表示“读取文件的内容并在此处插入”。

If the expression needed to determine the insertion point has to be more complex, please elaborate in comments. 如果确定插入点所需的表达式必须更复杂,请在注释中详细说明。 For example, "exactly an open parenthesis and nothing else" would be /^($/ 例如,“完全是一个开括号而不是别的”将是/^($/

EDIT thank you for clarifying the requirement. 编辑感谢您澄清要求。 If you need to insert this text right after example text followed by an ( , the following script ought to work. Put it in a file of its own, and make it executable ( chmod 755 myScript ), then run with ./myScript . 如果您需要在example text之后插入此文example text后跟一个( ,以下脚本应该工作。将它放在自己的文件中,并使其可执行( chmod 755 myScript ),然后使用./myScript运行。

#!/bin/bash
sed '
/exampletext/ {
  N
  /(/ r multi2.txt
}' multi1.txt

Explanation: 说明:

/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

Note that this produces output to stdout . 请注意,这会产生输出到stdout You could direct this to a new file name - for example 您可以将其指向新文件名 - 例如

./myScript > newFile.txt

I tested this with the following input file ( multi1.txt ): 我用以下输入文件( multi1.txt )测试了这个:

some text...
sometext...
sometext (sometext)....
  exampletext
  not the right string
    (
    );
sometext
  (
  );
  exampletext
    (
    );
sometext...

And it gave the output 它给出了输出

some text...
sometext...
sometext (sometext)....
  exampletext
  not the right string
    (
    );
sometext
  (
  );
  exampletext
    (
insert this
and that
    );
sometext...

Which I think is what you wanted? 我认为你想要的是什么? The text is inserted where the example text is followed by an open parenthesis - but not when there's another line in between... 文本插入example text后面跟一个左括号 - 但是当它们之间还有另一行时...

Here is another way of doing it with awk : 这是使用awk执行此操作的另一种方法:

awk '/^\(/{print $0; while((getline line <"filetoInsert") > 0) print line; next}1' firstFile

Explanation: 说明:

  • We use a simple regex /^\\(/ to state that look at the line that starts with an opening parenthesis. 我们使用一个简单的正则表达式/^\\(/表示查看以左括号开头的行。
  • Once we encounter that line, we print it. 一旦我们遇到那条线,我们就会打印出来。
  • We then use getline function to read through the second file. 然后我们使用getline函数来读取第二个文件。
  • Once it has been read and printed completely, we use next to avoid default printing of the line of original file (our paren) again as we have turned on default printing using the 1 you see at the end. 一旦完全读取和打印,我们next使用以避免默认打印原始文件行(我们的paren),因为我们使用您在最后看到的1打开默认打印。

这可能适合你(GNU sed):

sed -e '$!N;/^(\n);$/{r insert_file' -e '};P;D' first_file

also in python: 也在python中:

import sys

with open(sys.argv[2]) as secondFile:
    insertStrList=secondFile.readlines()

firstStrList=[]
with open(sys.argv[1]) as firstFile:
    while True:
        i_str=firstFile.readline()
        if not i_str: 
            break
        firstStrList.append(i_str)
        if i_str == '(\n':
            firstStrList+=insertStrList

print ''.join(firstStrList)

using it with python mergeParenthesis.py a.txt b.txt 使用它与python mergeParenthesis.py a.txt b.txt

But I agree Floris answer is easier ;) 但我同意Floris的答案更容易;)

cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM