使用组名称搜索并替换 UUID

发布于 2025-01-19 17:04:29 字数 1511 浏览 1 评论 0原文

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

$ 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 文件(采用下面给定的格式),我需要在所有存储库中搜索带有组名称的 groups 文件,例如 member/brazil/linux/ team 如果组名称存在于组文件中,则需要用新的 UUID 替换。

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

使用每个组的以下命令,我可以获得其新 UUID

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

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
............
............................

I have list of group name in the following file.

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

Then i have groups file (in below given format) in many of our git repositories and i need to search in all repositories of groups file with the group name for example member/brazil/linux/team if group name exists in group file, then it need to replace with new UUID.

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

With the below command for each group i get its new UUID

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

ef02b22ac4ce179a0064b1df2b326fd6b5dce514

Expected output for one groups file:-

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

Need help to replace each of my current UUIDs with the new ones in an automated way.

@tshiono, Requested output as follows.

$ 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
............
............................

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

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

发布评论

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

评论(1

泪痕残 2025-01-26 17:04:29

请您尝试一下 bash 脚本:

#!/bin/bash

declare -A ary                                  # use an associative array "ary"
while IFS=

使用提供的示例输出:

# UUID                                          Group Name
#
ef02b22ac4ce179a0064b1df2b326fd6b5dce514        member/brazil/linux/team
  • 它首先在 while 循环中读取 ssh 命令的输出,
    将行拆分为字段,并将两个变量 groupuuid 分配给第一个和第二个字段。然后将数组ary分配给由group索引的uuid。如果 ssh 的输出包含多行(多个 group-uuid 对),则数组将保存尽可能多的值。
  • ssh 的输出通过进程替换送入while循环
    语法为 < 的机制<(命令)
  • 第二个 while 循环处理文件 groups 替换 uuid
    与在第一个循环中分配的 group 关联。
  • 原始 groups 文件不会被覆盖。输出被重定向到一个新文件newgroups。如果文件看起来正确,请使用 mv -i newgroups groups 重命名。提前备份原groups文件。
\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

使用提供的示例输出:

  • 它首先在 while 循环中读取 ssh 命令的输出,
    将行拆分为字段,并将两个变量 groupuuid 分配给第一个和第二个字段。然后将数组ary分配给由group索引的uuid。如果 ssh 的输出包含多行(多个 group-uuid 对),则数组将保存尽可能多的值。
  • ssh 的输出通过进程替换送入while循环
    语法为 < 的机制<(命令)
  • 第二个 while 循环处理文件 groups 替换 uuid
    与在第一个循环中分配的 group 关联。
  • 原始 groups 文件不会被覆盖。输出被重定向到一个新文件newgroups。如果文件看起来正确,请使用 mv -i newgroups groups 重命名。提前备份原groups文件。

Would you please try the bash script:

#!/bin/bash

declare -A ary                                  # use an associative array "ary"
while IFS=

Output with the provided example:

# UUID                                          Group Name
#
ef02b22ac4ce179a0064b1df2b326fd6b5dce514        member/brazil/linux/team
  • It first reads the output of ssh command in the while loop,
    splitting the line into fields and assigning two variables group and uuid to the 1st and 2nd fields. Then an array ary is assigned to uuid indexed by group. If the output of ssh contains multiple lines (multiple group-uuid pairs), the array holds the values as many.
  • The output of ssh is fed to the while loop via the process substitution
    mechanism with the syntax < <(command).
  • The second while loop processes the file groups replacing the uuid
    associated with group, which is assigned in the first loop.
  • The original groups file is not overwritten. The output is redirected to a new file newgroups. If the file looks correct, rename it with mv -i newgroups groups. Backup the original groups file in advance.
\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

Output with the provided example:

  • It first reads the output of ssh command in the while loop,
    splitting the line into fields and assigning two variables group and uuid to the 1st and 2nd fields. Then an array ary is assigned to uuid indexed by group. If the output of ssh contains multiple lines (multiple group-uuid pairs), the array holds the values as many.
  • The output of ssh is fed to the while loop via the process substitution
    mechanism with the syntax < <(command).
  • The second while loop processes the file groups replacing the uuid
    associated with group, which is assigned in the first loop.
  • The original groups file is not overwritten. The output is redirected to a new file newgroups. If the file looks correct, rename it with mv -i newgroups groups. Backup the original groups file in advance.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文