使用组名搜索并替换 UUID

Search with group name and replace the UUID

我在以下文件中有组名列表。

$ cat group_list.txt
member/brazil/linux/team
member/london/windows/team
member/china/bootloader/team
member/india/message/team
member/romania/mac/team
........
...........

然后我在我们的许多 git 存储库中有 groups 文件(以下面给定的格式),我需要在组文件的所有存储库中搜索组名,例如 member/brazil/linux/team 如果组文件中存在组名,则需要替换为new UUID.

$ cat groups
# UUID                                          Group Name
#
b16e145bac197a36802a31c5886ad726ee4f38c4        member/brazil/linux/team

对每个组使用以下命令,我得到它的 new UUID

$ ssh -p 29418 review.example.com gerrit ls-groups -v | awk '-F\t' ' == "member/brazil/linux/team" {print }'

ef02b22ac4ce179a0064b1df2b326fd6b5dce514

一组文件的预期输出:-

$ cat groups
# UUID                                          Group Name
#
ef02b22ac4ce179a0064b1df2b326fd6b5dce514        member/brazil/linux/team

需要帮助以自动方式将我当前的每个 UUID 替换为新 UUID。

@tshiono,请求输出如下。

$ ssh -p 29418 review.example.com gerrit ls-groups -v
member/brazil/linux/team       b16e145bac197a36802a31c5886ad726ee4f38c4                member/brazil/linux/team       b16e145bac197a36802a31c5886ad726ee4f38c4        false
member/london/windows/team     3cab73598a48f443c8ca21fb77b1ea42ef00cbe6                member/london/windows/team     3cab73598a48f443c8ca21fb77b1ea42ef00cbe6        false
............
............................

你能试试bash脚本吗:

#!/bin/bash

declare -A ary                                  # use an associative array "ary"
while IFS=$'\t' read -r group uuid _; do        # loop over the output of `ssh`
    ary[$group]=$uuid                           # store the uuid indexed by the group
done < <(ssh -p 29418 review.example.com gerrit ls-groups -v)
                                                # feed the output of `ssh` to the while loop

while IFS= read -r line; do                     # loop over the lines of "groups" file
    if (( nr++ < 2 )); then                     # print the header lines "as is"
        echo "$line"
    else
        read -r uuid group <<< "$line"          # split the line into uuid and group
        if [[ -n ${ary[$group]} ]]; then        # if the group has the new uuid in the array "ary"
            uuid=${ary[$group]}                 # then overwrite the uuid with it
        fi
        printf "%s\t%s\n" "$uuid" "$group"      # print the result line
    fi
done < groups > newgroups

提供示例的输出:

# UUID                                          Group Name
#
ef02b22ac4ce179a0064b1df2b326fd6b5dce514        member/brazil/linux/team
  • 它首先在while循环中读取ssh命令的输出, 将行拆分为字段并将两个变量 groupuuid 分配给第一个和第二个字段。然后将一个数组 ary 分配给由 group 索引的 uuid。如果 ssh 的输出包含多行(多个 group-uuid 对),则该数组将包含许多值。
  • ssh 的输出通过 process substitution 馈送到 while 循环 语法为 < <(command).
  • 的机制
  • 第二个 while 循环处理文件 groups 替换 uuid 与在第一个循环中分配的 group 关联。
  • 原来的groups文件没有被覆盖。输出被重定向到一个新文件 newgroups。如果文件看起来正确,请将其重命名为 mv -i newgroups groups。提前备份原groups文件