在bash中解析.ini文件 [英] Parsing .ini file in bash

查看:131
本文介绍了在bash中解析.ini文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的属性文件,并想按如下所述对其进行解析.请帮助执行此操作.

I have a below properties file and would like to parse it as mentioned below. Please help in doing this.

.ini文件:

[Machine1]

app=version1


[Machine2]

app=version1

app=version2

[Machine3]

app=version1
app=version3

我正在寻找一种解决方案,其中应将ini文件解析为

I am looking for a solution in which ini file should be parsed like

[Machine1]app = version1
[Machine2]app = version1
[Machine2]app = version2
[Machine3]app = version1
[Machine3]app = version3

谢谢.

推荐答案

尝试:

$ awk '/\[/{prefix=$0; next} $1{print prefix $0}' file.ini
[Machine1]app=version1
[Machine2]app=version1
[Machine2]app=version2
[Machine3]app=version1
[Machine3]app=version3

工作原理

  • /\[/{prefix=$0; next}

如果有任何行以[开头,则将该行保存在变量prefix中,然后跳过其余命令,并跳转到next行.

If any line begins with [, we save the line in the variable prefix and then we skip the rest of the commands and jump to the next line.

$1{print prefix $0}

如果当前行不为空,则我们将在当前行之后打印前缀.

If the current line is not empty, we print the prefix followed by the current line.

要在出现=的任何地方添加空格:

To add spaces around any occurrence of =:

$ awk -F= '/\[/{prefix=$0; next} $1{$1=$1; print prefix $0}' OFS=' = ' file.ini
[Machine1]app = version1
[Machine2]app = version1
[Machine2]app = version2
[Machine3]app = version1
[Machine3]app = version3

这通过使用=作为输入上的字段分隔符和=作为输出上的字段分隔符来工作.

This works by using = as the field separator on input and = as the field separator on output.

这篇关于在bash中解析.ini文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆