Bash 在模式和行中搜索并替换为sed by
我读过很多类似的文章,但没有一篇能适用于我的 我希望在某些特定行中使用sed进行搜索和替换,以便只匹配第一次出现的;假设我有脚本的这一部分:Bash 在模式和行中搜索并替换为sed by,bash,sed,replace,Bash,Sed,Replace,我读过很多类似的文章,但没有一篇能适用于我的 我希望在某些特定行中使用sed进行搜索和替换,以便只匹配第一次出现的;假设我有脚本的这一部分: processor <- read.table("../mall_all/adpcm/FULL_DB-constprop", header=TRUE, colClasses=c("reassociate"="factor", "scalarrepl"="factor", "inline"="factor", "sccp"="factor", "lo
processor <- read.table("../mall_all/adpcm/FULL_DB-constprop", header=TRUE, colClasses=c("reassociate"="factor", "scalarrepl"="factor", "inline"="factor", "sccp"="factor", "loop_reduce"="factor"))
processor<-processor[-c(20:40)]
processor$intensity <- processor$int_high - processor$int_low
processor$performance<- processor$perf_high - processor$perf_low
processor<-processor[-c(1:4)]
processor<-processor[,!names(processor) %in% c("constprop")]
在我写的一个循环中,问题是;我希望colClasses参数和其余脚本在进入循环时保持不变(循环有编译器选项,如:重新关联、内联、constprop等)
我想知道为什么我的搜索和替换不起作用:
set -x
compilerOptionList="constprop dce inline instcombine licm loop_reduce loop_rotate loop_unroll loop_unswitch loop_unswitch mem2reg memcpyopt reassociate scalarrepl sccp simplifycfg "
stringToBeReplaced=constprop
for compilerOption in $compilerOptionList
do
echo "Using compiler option: $compilerOption"
//here you could see the sed scripts
sed -i "1,15 /FULL_DB/,/header/ s/$stringToBeReplaced/$compilerOption/" r.scr
stringToBeReplaced=$compilerOption
make
mv Rplots.pdf Rplots_adpcm_$compilerOption.pdf
echo "DONE! $compilerOption"
done
谢谢大家的时间和帮助;)
阿米尔我不确定是否正确理解了您的需求,但可能是这样的
sed -e "
1,15ba;
/FULL_DB/,/header/ba;
bb;
:a;
s/stringToBeReplaced/$compilerOption/;
:b;
" -i r.scr
我能做这项工作。这行有问题
sed -i "1,15 /FULL_DB/,/header/ s/$stringToBeReplaced/$compilerOption/" r.scr
这不是有效的sed命令语法。你需要像这样用大括号把它的一部分围起来
sed -i "1,15 { /FULL_DB/,/header/ s/$stringToBeReplaced/$compilerOption/ }" r.scr
但我认为一种更整洁的方法是使用单独的文件来输入和输出
sed
,即将该行更改为
sed "1,15 s/constprop/$compilerOption/" r.scr_tmp >r.scr
您不需要stringtobereplace
变量。这样,您总是替换“constprop”,并且不必担心要替换的字符串会出现在代码的其他地方
r.scr\u tmp
将包含与r.scr
相同的代码,除了r.scr\u tmp
的constprop
部分保持不变。伙计们,我刚刚发现我可以使用两个sed。i、 例如:sed-i“2,3s/$stringtobereplace/$compilerOption/$r.scr sed-i”9,11s/$stringtobereplace/$compilerOption/$r.scrBut,无论如何,如果我能使用一个sed,我会很感激的(我会学到一些新的;)
sed "1,15 s/constprop/$compilerOption/" r.scr_tmp >r.scr