如何使用`yq`选择键值对并将其格式化为“$key=$value”风格输出?

发布于 2025-01-09 17:37:13 字数 475 浏览 1 评论 0原文

假设我有如下所示的 YAML 文件:

FOO: somefoo
BAR: somebar

我想将其转换(使用 yq) 为以下内容,以便我可以将内容源到环境变量中:

export BAR='somebar'
export FOO='somefoo'

我可以使用 jq 首先将输入转换为 JSON,但我似乎无法弄清楚如何仅使用 yq 来实现。 (我正在使用 yq 4.x,<4.18)。

那么,具体来说,我如何仅使用 yq 来执行以下操作?

INPUT="FOO: somefoo
BAR: somebar"

echo "$INPUT" | yq e 'to_json' - | jq -r 'keys[] as $k | "export \($k)='\''\(.[$k])'\''"'

Let say I have YAML file that looks like this:

FOO: somefoo
BAR: somebar

I would like to convert this (using yq) into the following so that I can source the contents into environment variables:

export BAR='somebar'
export FOO='somefoo'

I can do it it with jq by converting the input to JSON first, but I can't seem to figure out how to do it with yq only. (I am using yq 4.x, <4.18).

So, concretely, how could I do the following using just yq?

INPUT="FOO: somefoo
BAR: somebar"

echo "$INPUT" | yq e 'to_json' - | jq -r 'keys[] as $k | "export \($k)='\''\(.[$k])'\''"'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想你只要分分秒秒 2025-01-16 17:37:13

您可以切换到 kislyuk 的 yq 它在底层使用本机 jq。然后,您只需要 to_entries 要访问键和值,字符串插值-r标志结合产生输出,以及@sh 转义以符合 shell 要求:

yq -r 'to_entries[] | "export \(.key)=\(.value | @sh)"'
export FOO='somefoo'
export BAR='somebar'

You could switch to kislyuk's yq which uses native jq under the hood. Then, you would just need to_entries to access key and value, string interpolation in combination with the -r flag to produce the output, and @sh to escape for shell compliance:

yq -r 'to_entries[] | "export \(.key)=\(.value | @sh)"'
export FOO='somefoo'
export BAR='somebar'
池予 2025-01-16 17:37:13

@sh 运算符已添加到 yq v4.31.1(与我谦虚的贡献)。现在您可以按照与 jq 中的方式几乎相同的方式进行操作:

yq '.[] | "export " + key + "=" + @sh'

引用算法与 jq 略有不同,因为它仅在需要引用的字符处开始引用,因此文字输出可能会有所不同,但稍后会被同等地解析。

# input
FOO: somefoo
BAR: somebar and some
# result
export FOO=somefoo
export BAR=somebar' and some'

使用yq版本,您仍然可以使用其他yq函数实现原始但安全的引用算法(尽管引用是一个可爱的噩梦) :

# POSIX shell quoting starting "
yq ".[] | \"export \" + key + \"='\" + sub(\"'\", \"'\''\") + \"'\""
# POSIX shell quoting starting '
yq '.[] | "export " + key + "='\''" + sub("'\''", "'\'\\\'\''") + "'\''"'
# BASH "dollar" quoting
yq 

实际上这正是 什么jq 使用它的 @sh 来实现。在所有情况下,最终结果都是:

export FOO='somefoo'
export BAR='somebar and some'
.[] | "export " + key + "=\'" + sub("\'", "\'\\\'\'") + "\'"'

实际上这正是 什么jq 使用它的 @sh 来实现。在所有情况下,最终结果都是:

The @sh operator has been added in yq v4.31.1 (with my humble contribution). Now you can do it pretty much the same way as in jq:

yq '.[] | "export " + key + "=" + @sh'

The quoting algorithm is a bit different from jq as it starts to quote only at characters that need quoting, so the literal output will likely differ, but will be later parsed equally.

# input
FOO: somefoo
BAR: somebar and some
# result
export FOO=somefoo
export BAR=somebar' and some'

With older yq versions you can still implement a primitive but safe quoting algorithm using other yq functions (the quoting is a little lovely nightmare, though):

# POSIX shell quoting starting "
yq ".[] | \"export \" + key + \"='\" + sub(\"'\", \"'\''\") + \"'\""
# POSIX shell quoting starting '
yq '.[] | "export " + key + "='\''" + sub("'\''", "'\'\\\'\''") + "'\''"'
# BASH "dollar" quoting
yq 

Actually this is exactly what jq does with its @sh. In all cases this ends up as:

export FOO='somefoo'
export BAR='somebar and some'
.[] | "export " + key + "=\'" + sub("\'", "\'\\\'\'") + "\'"'

Actually this is exactly what jq does with its @sh. In all cases this ends up as:

千柳 2025-01-16 17:37:13

使用 key 运算符和字符串连接:

$ echo "$INPUT" | yq 

使用 yq 4.27.2 进行测试

.[] | "export " + key + "=\'" + . + "\'"' export FOO='somefoo' export BAR='somebar'

使用 yq 4.27.2 进行测试

Use the key operator and string concatenation:

$ echo "$INPUT" | yq 

Tested with yq 4.27.2

.[] | "export " + key + "=\'" + . + "\'"' export FOO='somefoo' export BAR='somebar'

Tested with yq 4.27.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文