Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在寻找一个Perl或sed一行程序,用另一个文件的内容替换字符串_Perl_String_File_Replace_Sed - Fatal编程技术网

正在寻找一个Perl或sed一行程序,用另一个文件的内容替换字符串

正在寻找一个Perl或sed一行程序,用另一个文件的内容替换字符串,perl,string,file,replace,sed,Perl,String,File,Replace,Sed,在一个HTML文件中,我们称它为index.HTML,我想用一个单独文件的内容(多行)替换一个注释字符串,比如说//gac goes here,该文件名为:gac.js。有好的一条邮轮吗 我发现了如下内容:sed-e/$str/r FileB“-e”/$str/d”FileA,但它没有按照承诺工作 我确实希望它尽可能短,因为它在恢复后会被调用(我不希望任何google.script污染我的开发环境)。不太好,但似乎可以工作: cat index.html | perl -pe 'open(GAC

在一个HTML文件中,我们称它为
index.HTML
,我想用一个单独文件的内容(多行)替换一个注释字符串,比如说
//gac goes here
,该文件名为:gac.js。有好的一条邮轮吗

我发现了如下内容:
sed-e/$str/r FileB“-e”/$str/d”FileA
,但它没有按照承诺工作


我确实希望它尽可能短,因为它在恢复后会被调用(我不希望任何google.script污染我的开发环境)。

不太好,但似乎可以工作:

cat index.html | perl -pe 'open(GAC, "gac.js");@gac=<GAC>;$data=join("", @gac); s/gac goes here/$data/g'
cat index.html | perl-pe'open(GAC,“GAC.js”)@gac=$数据=连接(“,@gac”);s/gac在这里/$data/g'

这应该是可行的,即使它很糟糕:

perl -pe 'BEGIN{open F,"gac.js";@f=<F>}s#//gac goes here#@f#' index.html

显然需要文件::Slurp

在经历了
手动sed
、教程和我想到的一些实验之后:

sed -i '\_//gac goes here_ {
    r gac.js
    d 
}' index.html
这正是我想要的。这并不是我不理解的一行代码(如果我只写一行:
sed:-e表达式#1,char 0:unmatched'{'
),但是上面的表达式很适合我的更新脚本


经验教训:sed功能非常强大,-i在mac os x/linux上的行为不同,/string/可以很容易地替换为\[other delimiter]string[other delimiter]。

文件gac.js的内容是多行还是单行?gac.js是多行(已编辑)那个sed命令对我很有用。出了什么问题了?sed不是很有经验,但我认为我用斜杠做了些错事。我认为gac应该是动态的。cat的无用用法()每次你从
index.html
@Chas-Owens中读到一行时,你都会读到
gac.js
,你的版本更好:)。+1因为“{”不匹配的原因是sed将从r到换行符的所有内容都作为文件名,所以右大括号被解释为文件名的一部分。谢谢你的这个讨厌的一行!
perl -mFile::Slurp -pe 's/\/\/(\w+) goes here/@{[File::Slurp::read_file("$1.js")]}/;'
sed -i '\_//gac goes here_ {
    r gac.js
    d 
}' index.html