linux - 如何循环输出并将结果格式化为新报告?
问题描述
我正在编写一个脚本来处理配置文件的输出,从而产生有价值的数据。
输出格式为:
[header]
attribute1 = integer
attribute2 = integer
[header]
attribute1 = integer
attribute2 = integer
...
可能有未知数量的节具有相同的两个属性(具有未知的整数值),但具有不同的标题。
到目前为止,我只能生成不同节的数量以将其用作循环计数器,但我不确定如何循环输出,保持标题不变,但将两个属性的整数值相加并替换这些具有总和的新属性,例如,
[header]
new_attribute = integer
[header]
new_attribute = integer
我已经查看了该命令read
,但我不确定如何使用它生成我想要的报告。
解决方案
while read header &&
read name1 equal value1 &&
read name2 equal value2
do
echo "$header"
echo "new_attribute = $((value1 + value2))"
done < input.cfg > output.cfg
此代码假定输入完全符合禁止的格式。它不能稳健地处理错误输入:格式错误的行、丢失的行、意外的反斜杠等。
推荐阅读
- java - 未经检查地调用 ... 作为原始类型的成员
- c++ - C++ STL 向量前值未清除
- laravel - 使用 Comeonin 将 bcrypt 密码哈希从 Laravel 应用程序迁移到 Elixir 应用程序
- python - Python 3 通过 readline 将空字符串添加到列表中
- python - 如何展平熊猫数据框的每一行?
- javascript - 需要使用Javascript在给定日期的日历季度中查找周数
- xml - PLOT TWIST:如何组合多个 XML 文件并输出多个节点?
- rest - 检查 POST 服务方法中的 API 请求限制是否违反 REST?
- excel - 为什么这个对多维 vba 数组的简单赋值会引发语法错误
- ios - 尝试运行单元测试并收到此消息 Use of undeclared identifier 'QRCLocalizedLabels' using XCode 10 Beta 4 and iOS 12 Beta 6