我有这样的数据:
['/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom5/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom6/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom7/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom8/', '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom9/']
我希望每行每个值都没有引号,以便我可以将其通过管道传输到另一个命令,例如:
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/
....
awk -F'[][]' '{print $2}'
删除方括号。我不明白如何进一步进行。
答案1
您可以删除前导['
、尾随']
并', '
用换行符替换所有内容,然后删除我看到的用于表示空字符串列表的[]
或行(其中前缀指定该数组是一个字符串数组)。@as []
gsettings
@as
a
s
gsettings get some-path some-array-key... |
sed "/^@as \[\]\$/d
/^\[\]\$/d
s/^\['//
s/'\]\$//
s/', '/\\
/g"
使用 GNU sed
,上面最后一个命令中替换中的换行符s
可以表示为\n
而不是\
后面跟着文字换行符。
如果不是空列表的'...'
替代"..."
和那个@as
前缀,那将是一个有效的 JSON 数组,所以你也可以这样做:
gsettings get some-path some-array-key |
sed "y/'/\"/; s/^@as //" |
jq -r '.[]'
gsettings
请注意,这些都不适用于任意字符串数组,因为它们没有考虑可以通过以下方式完成的转义:
$ gsettings list-recursively | grep '\\'
org.freedesktop.ibus.panel.emoji favorites ['\u200b']
org.gnome.evolution.shell filename-illegal-chars "'\"&`();|<>$%{}!"
(我们甚至可以看到它切换到"..."
最后一种情况,其中字符串值包含 a '
,在这种情况下使其成为正确的 JSON 字符串)。
$ gsettings set org.gnome.seahorse last-search-text $'\1\xa\U10FFFF\'\"\\'
$ gsettings get org.gnome.seahorse last-search-text
"\u0001\n\U0010ffff'\"\\"
格式似乎是描述的格式https://developer-old.gnome.org/glib/stable/gvariant-text.html
该JSON:PP
perl
模块支持使用单引号分隔符的 JSON 字符串,因此您可以使用它来提取和解码任何 GVariant 对象中的任何单引号或双引号字符串,如下所示:
gsettings any-path any-key |
perl -C -MJSON::PP -lne '
BEGIN{
$j = JSON::PP->new->allow_singlequote
}
print $j->decode($_) for
/'\''(?:\\.|[^\\'\''])*'\''|"(?:\\.|[^\\"])*"/g'
正则表达式找到一对'...'
或"..."
(允许内部转义引号或反斜杠),并将其传递给 JSON 解码器。忽略JSON:PP 会阻塞为 AFAICT 的 , 字节串,b'...'
将其输出为。b"..."
gsettings
[byte 0xHH, 0xHH...]
答案2
使用 GNU grep-o
并假设引用的字符串不包含任何字符][ ,'
或换行符:
$ grep -Eo "[^][ ,']+" file
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom5/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom6/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom7/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom8/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom9/
否则使用任何 awk 并允许'
在引用的字符串中使用除换行符之外的任何字符:
$ awk -F"'" '{for (i=2; i<NF; i+=2) print $i}' file
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom0/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom1/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom2/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom3/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom4/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom5/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom6/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom7/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom8/
/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom9/