查找和替换其他行

时间:2018-08-24 06:46:40

标签: regex perl sed

让我们说几行,我必须在大括号内选择一个字符串,然后在几行后用它替代其他地方

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "responseStatusCode", "requestIdentifier", "primitiveContent", "to", "from","originatingTimestamp", "resultExpirationTimestamp", "eventCategory" })
    //@XmlRootElement(name = "rsp", namespace = "http://www.onem2m.org/xml/protocols") //(change)
    @JsonInclude(Include.NON_EMPTY)
    @JsonRootName("m2m:rsp")
    public class ResponsePrimitive {
      @XmlElement(name = "rsc")
      private BigInteger responseStatusCode;
      @XmlElement(name = "rqi")
      private String requestID;
       //@XmlElement(name = "pc")  //(change)
      private PrimitiveContent primitiveContent;
      @XmlElement(name = "to")
      private String to;
      @XmlElement(name = "fr")
      private String from;
      @XmlElement(name = "ot")
      private String originatingTimestamp;
      @XmlElement(name = "rset")
      private String resultExpirationTimestamp;
      @XmlElement(name = "ec")
      private String eventCategory;

我想选择Here is the word that I need to select in braces (WORD) Second line Third line Forth line Path where I want it to replace /root/usr/destination/ few more lines 并将WORD替换为destination 看起来像这样

WORD

2 个答案:

答案 0 :(得分:1)

默认情况下,sed逐行工作。因此,为了使用该模式,您应该使其与多行一起使用。 See this Stackoverflow question

一个更简单的选择是使用perl。 Perl附带了几乎所有(如果不是全部)的Linux发行版和一些UNIX系统。

因此您可以使用此:

perl -0777 -pe 's/([\s\S]*?\()([^)]+)(\)[\s\S]*?\/root\/usr\/)destination([\s\S]*?)/$1$2$3$2$4/g' file.txt

正则表达式是这个:

([\s\S]*?\()([^)]+)(\)[\s\S]*?\/root\/usr\/)destination([\s\S]*?)

替换为:$1$2$3$2$4

Demo.

答案 1 :(得分:1)

我担心您发明的输入数据,因为它似乎与您评论中的“答案”可能无法正确处理的任何东西都差不多。但是,这是一个从集成DATA文件句柄读取的解决方案。我一次只读取了一行文件,而不是一味地读一遍,因为这样可以更容易地拾取新的替换字符串

唯一的其他条件是

  • destination关键字永远不会出现在替换行的之前

  • 无需区分路径字符串中destination的出现

  • 输入文本中没有括号,但替换单词周围没有括号

use strict;
use warnings 'all';

my $rep;

while ( <DATA> ) {
    $rep = $1 if /\(([^()]+)\)/;
    s/destination/$rep/g if defined $rep;
    print;
}


__DATA__
Here is the word that I need to select in braces (WORD1)
Second line
Third line
Forth line 
Path where I want it to replace /root/usr/destination/
few more lines

Here is the word that I need to select in braces (WORD2)
Second line
Third line
Forth line 
Path where I want it to replace /root/usr/destination/
few more lines

Here is the word that I need to select in braces (WORD3)
Second line
Third line
Forth line 
Path where I want it to replace /root/usr/destination/
few more lines

输出

Here is the word that I need to select in braces (WORD1)
Second line
Third line
Forth line
Path where I want it to replace /root/usr/WORD1/
few more lines

Here is the word that I need to select in braces (WORD2)
Second line
Third line
Forth line
Path where I want it to replace /root/usr/WORD2/
few more lines

Here is the word that I need to select in braces (WORD3)
Second line
Third line
Forth line
Path where I want it to replace /root/usr/WORD3/
few more lines

尽管我建议您不要使用它们,但单线版本是

perl -pe '$rep = $1 if /\(([^()]+)\)/; s/destination/$rep/g if defined $rep;' myfile