bash - Bash - 从特定字符串开始删除文件的全部内容
问题描述
我在 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
解决方案
使用 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"
请注意,我双引号引用了变量,这是一个很好的做法。
推荐阅读
- javascript - 拖放时,新元素替换现有元素并将旧元素放回列表中
- java - 在 Spring Boot 中创建 ResponseEntity 类的对象的方法
- javascript - 将输入的密码与存储的密码进行比较
- android - 底部导航 GONE
- javascript - 如何根据 URL 设置样式?
- listview - 编辑文本列表视图中的小键盘
- python - 1 个数组中的多个 2D numpy 数组
- vb.net - 填充图片框的次数与我的 csv 中的行数一样多
- android - 在 Android 本机中访问使用 Flutter 创建的数据库时出现 SQLiteException
- r - 如何区分扫描的 PDF 和常规文本 PDF