【问题标题】:Add text between two patterns in File using sed command使用 sed 命令在 File 中的两个模式之间添加文本
【发布时间】:2017-07-26 23:52:12
【问题描述】:

我想在两个模式之间添加一些大代码:

文件 1.txt

This is text to be inserted into the File.

infile.txt

Some Text here
First
Second
Some Text here

我想在 FirstSecond 之间添加 File1.txt 内容:

期望的输出:

Some Text here
First
This is text to be inserted into the File.
Second
Some Text here

我可以使用 sed 命令使用两种模式进行搜索,但我不知道如何在它们之间添加内容。

sed '/First/,/Second/!d' infile 

【问题讨论】:

    标签: linux unix sed


    【解决方案1】:

    因为/r 代表读取文件,所以使用:

    sed '/First/r file1.txt' infile.txt
    

    您可以在这里找到一些信息:Reading in a file with the 'r' command

    为就地版本添加-i(即sed -i '/First/r file1.txt' infile.txt)。

    要执行此操作,无论字符大小写如何,请按照Use sed with ignore case while adding text before some pattern 中的建议使用I 标记:

    sed 's/first/last/Ig' file
    

    如 cmets 所示,上述解决方案只是在一个模式之后打印给定的字符串,而不考虑第二个模式。

    为此,我会选择带标志的 awk:

    awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
    

    鉴于这些文件:

    $ cat patt_file
    This is text to be inserted
    $ cat file
    Some Text here
    First
    First
    Second
    Some Text here
    First
    Bar
    

    让我们运行命令:

    $ awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
    Some Text here
    First                             # <--- no line appended here
    First
    This is text to be inserted       # <--- line appended here
    Second
    Some Text here
    First                             # <--- no line appended here
    Bar
    

    【讨论】:

    • 如果您再次阅读该问题,@Mann 正在寻找在FistSecond 之间插入内容只需考虑以下文件以及命令Some Text here First @987654336 @SecondSome Text here我不确定你提到的代码是否能满足用户的要求。
    • @AlexRajKaliamoorthy 好吧,这是一个边缘案例,显然与 Mann 无关,因为他将答案标记为已接受。如果有必要,一些awk 会做得更好。
    • 确实解决方案有问题,因为它永远不会替换两行之间的文本,而是在第一行之后不断添加文本。
    • @Droidzone 我重新审视了这个案例的答案。谢谢提醒!
    • @AlexRajKaliamoorthy 我刚刚添加了一个带有标志的awk 变体来涵盖这一点。感谢您的提醒!
    【解决方案2】:

    我觉得你可以试试这个

    $ sed -n 'H;${x;s/Second.*\n/This is text to be inserted into the File\
    &/;p;}' infile.txt
    

    【讨论】:

    • 这不是一个解决方案,因为您没有阅读两行之间的文本。
    【解决方案3】:

    awk风味:

    awk '/First/ { print $0; getline < "File1.txt" }1' File2.txt
    

    【讨论】:

      【解决方案4】:

      这是我为从 patt_file 插入模式而编写的一段 bash 代码。基本上不得不使用 uniq 删除一些重复的数据,然后重新添加一些东西。我使用 lineNum 值复制我需要放回的东西,将其保存到 past_file。然后在我要添加内容的文件中匹配 patMatch。

         #This pulls the line number from row k, column 2 of the reduced repitious file       
         lineNum1=$(awk -v i=$k -v j=2 'FNR == i {print $j}' test.txt)    
      
          #This pulls the line number from row k + 1, coulmn 2 of the reduced repitious file
          lineNum2=$(awk -v i=$((k+1)) -v j=2 'FNR == i {print $j}' test.txt)
      
          #This pulls fields row 4, 2 and 3 column into with tab spacing (important) from reduced repitious file 
          awk -v i=$k -v j=2 -v h=3 'FNR == i {print $j"  "$h}' test.txt>closeJ.txt
      
          #This substitutes all of the periods (dots) for \. so that sed will match them
          patMatch=$(sed 's/\./\\./' closeJ.txt)
      
          #This Selects text in the full data file between lineNum1 and lineNum2 and copies it to a file 
      
          awk -v awkVar1=$((lineNum1 +1)) -v awkVar2=$((lineNum2 -1)) 'NR >= awkVar1   && NR <= awkVar2 { print }' nice.txt >patt_file.txt 
      
          #This inserts the contents of the pattern matched file into the reduced repitious file              
          #The reduced repitious file will now grow
          sed -i.bak "/$patMatch/ r "patt_file.txt"" test.txt
      

      【讨论】:

        猜你喜欢
        • 2018-08-24
        • 1970-01-01
        • 2017-12-26
        • 2018-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多