使用 sed 从源文本文件中查找和替换文本 [英] using sed to find and replace text from a source text file
本文介绍了使用 sed 从源文本文件中查找和替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试替换文本文件的一部分.再加一点文字
I'm trying to replace a part of text file. with another bit of text
源文件:
/qwertyae
目标文件:
abc
def
efg
! unique start
/qwertyad*.$fff|$sdf|$kkr.exe|these.stay.put
! unique end
基本上用/qwertyae 替换/qwertyad(同时保持其余行完好无损).使用 Sed(或其他方法)专门在 !独特的开始和!唯一的结尾(只需替换这两个标签之间的文本).
Basically replace /qwertyad with /qwertyae (while keeping the rest of the lines intact). Using Sed (or another method) to insert the source file specifically between ! unique start and ! unique end (and just replace the text between these 2 labels).
类似于 "sed -i 's/qwertyae/qwertyad/g' file.txt"
但可以使用文本文件源吗?
something like "sed -i 's/qwertyae/qwertyad/g' file.txt"
but can use a text file source?
推荐答案
$ 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
这篇关于使用 sed 从源文本文件中查找和替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文