bash: 基于字符串值的条目过滤

bash: string value based entries filtering

我的脚本中有这个检查:

[[ $KEY == contact@(s|_groups) ]] && CONFIG[$KEY]="$VALUE"

正在将包含联系人* 的行从一个文件写入数组。我如何添加另一个检查以跳过该行中的 xi* 值并将其写入数组?

我试过类似的方法:

[[ $KEY == contact@(s|_groups) ]] && [[ $VALUE != "xi*" ]] && CONFIG[$KEY]="$VALUE"

但这对我不起作用。 :/

The first file looks like this:

…
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick,ximgersic
…

The second file needs to look like this:

…
    contacts                        Marko Geršić,Mijo,nagiosadmin,Patrick
…

So, without the xi* in the contact* lines.

由于xi*位于$VALUE的末尾,您可以简单地使用bash Parameter Expansion 删除匹配的后缀模式

  [[ $KEY == contact@(s|_groups) ]] && CONFIG[$KEY]="${VALUE%,xi*}"

xi* values aren´t always at the end of the line

如果 xi*$VALUE 元素中间,您可以使用 模式替换:

  [[ $KEY == contact@(s|_groups) ]] && CONFIG[$KEY]="${VALUE/,xi*([^,])}"

and if there are multiple xi* values?

要删除多个 xi* 元素,只需将上面的 / 加倍即可:

  [[ $KEY == contact@(s|_groups) ]] && CONFIG[$KEY]="${VALUE//,xi*([^,])}"

[[ $KEY == contact@(s|_groups) ]] && CONFIG[$KEY]="${VALUE//xi*([^,])}" && CONFIG[$KEY]="${VALUE//,xi*([^,])}"

这张支票给了我想要的结果。 :)