[英]Execute a cat command within a sed command in Linux
我有一个包含一些内容的file.txt
。 我想在file1.txt
搜索一个字符串,如果该字符串匹配,我想用file.txt
的内容替换该字符串。 我怎样才能做到这一点?
我试过使用sed
:
sed -e 's/%d/cat file.txt/g' file1.txt
这是在file1.txt
搜索匹配的字符串并将其替换为字符串cat file.txt
,但我想要file.txt
内容。
在将文件插入到 sed 字符串之前,如何将文件的内容保存在变量中?
$content=`cat file.txt`; sed "s/%d/${content}/g file1.txt"
给定带有内容的original.txt
:
this is some text that needs replacement
和带有内容的replacement.txt
:
no changes
我不确定您的替换标准是什么,但假设我想替换原始文件中所有出现的单词replacement
,您可以使用其他文件的内容替换原始文本的内容:
> sed -e 's/replacement/'"`cat replacement.txt`"'/g' original.txt
this is some text that needs no changes
您可以使用sed
使用r
命令读取文件。 但是,这是基于行的操作,这可能不是您所追求的。
sed '/%d/r file1.txt'
读取发生在“每行”循环结束时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.