我在一个yml文件中有一个包含:# base config的字符串。现在,我想从这个文件中删除从这个特定字符串开始的所有内容。我也想删除这个字符串。有相应的命令吗?
bash文件
for filename in ./config/*.yml; do
if grep -qxF "# base config" $filename
then
echo "has base config, will replace it"
sed -i "" "# base config/q" $filename # this does not do anything
else
echo "has NOT base config, will add it"
cat $base_config >> $filename
fi
done发布于 2020-04-10 00:33:28
使用sed:
sed -n '/#base config/q;p' file或者使用awk:
awk '/#base config/{exit};1' file因此,您可以将脚本中的sed行替换为以下两行:
sed -n '/#base config/q;p' "$filename" > tmpfile
mv tmpfile "$filename"请注意,我用双引号引用了变量,这是很好的做法。
https://stackoverflow.com/questions/61125786
复制相似问题