使用sed查找和替换源文本文件中的文本

时间:2018-03-20 10:10:36

标签: sed

我正在尝试替换部分文本文件。用另一段文字

源文件:

abc
def
efg
! unique start
/qwertyad*.$fff|$sdf|$kkr.exe|these.stay.put
! unique end

目标文件:

"sed -i 's/qwertyae/qwertyad/g' file.txt"

基本上用/ qwertyae替换/ qwertyad(同时保持其余的线完好无损)。使用Sed(或其他方法)专门插入源文件!独特的开始和!唯一的结尾(只需替换这两个标签之间的文字)。

类似于SELECT LISTAGG (case,'+') WITHIN GROUP (ORDER BY case) case, ACC FROM TEST GROUP BY ACC ,但可以使用文本文件源吗?

1 个答案:

答案 0 :(得分:0)

$ sed -i '/unique start/,/unique end/{h;n;s/qwertyad/qwertyae/}' file

脚本版本(评论):

#!/usr/bin/sed -f

/unique start/,/unique end/{    # match /string from/, /to another string/
        h;                      # hold in the hold buffer
        n;                      # skip to next line
        s/qwertyad/qwertyae/    # classic substitution
}                               # end of commands group

修改过的文件:

abc
def
efg
! unique start
/qwertyae*.$fff|$sdf|$kkr.exe|these.stay.put
! unique end
相关问题