我正在编写一个脚本来替换 C++ 代码中的三行输入。这是我想要更改其输入的 C++ 代码片段:
char outputFileName_ForBlueEdge[50] = "BlueEdge_SetA_PeriodRange1.dat"; //File with model parameters on the blue edge
char outputFileName_ForPositiveGrowthModels[50] = "PostiveGrowth_SetA_PeriodRange1.dat"; //File with model parameters that have positve growth rates
char log_directory_prefix[30] = "LOGS_A/LOGS_A"; //Prefix to log_directory, suffix is model number. This is where LINA file should be
这是 bash 脚本的最小可重现版本:
dir=$PWD
cplusplus_plotter="$dir"/BlueEdge_Plotter_V7.cpp
#Set B
sed -i \
-e "s/^\([[:blank:]]*char outputFileName_ForBlueEdge[50]\).*/\1 = "BlueEdge_SetB_PeriodRange1.dat"/i" \
-e "s/^\([[:blank:]]*char outputFileName_ForPositiveGrowthModels[50]\).*/\1 = "PostiveGrowth_SetB_PeriodRange1.dat"/i" \
-e "s/^\([[:blank:]]*log_directory_prefix[30]\).*/\1 = "LOGS_B/LOGS_B"/i" \
"$cplusplus_plotter"
但是,C++ 文件永远不会改变,并且我不断收到错误:
sed: -e expression #3, char 59: unknown option to `s'
答案1
像这样:
sed -i \
-e 's/^\([[:blank:]]*char outputFileName_ForBlueEdge\[50\]\).*/\1 = "BlueEdge_SetB_PeriodRange1.dat"/i' \
-e 's/^\([[:blank:]]*char outputFileName_ForPositiveGrowthModels\[50\]\).*/\1 = "PostiveGrowth_SetB_PeriodRange1.dat"/i' \
-e 's!^\([[:blank:]]*log_directory_prefix\[30\]\).*!\1 = "LOGS_B/LOGS_B"!i' \
"$cplusplus_plotter"
您可以选择所需的分隔符(在 ascii 表中)。
我选择!
最后一个 sed 命令,因为你有 4 /
。