1

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
............
............................
user4948798
  • 1,924
  • 4
  • 43
  • 89
  • `With the below command for each group i get its new UUID`. Which file contains the new UUID? – tshiono Apr 07 '22 at 06:26
  • i have run the above command with `group name` to get the new UUID for each group name. – user4948798 Apr 07 '22 at 06:30
  • I don't get it. As you know the shown `awk` command is incomplete because the input file name (or piped input) is missing. How did you run it? Please provide the actiual command(s) you executed. – tshiono Apr 07 '22 at 06:39
  • Sorry for incomplete data, i have updated in question. Please have a look. – user4948798 Apr 07 '22 at 06:43
  • @tshiono, Someone has closed saying as `duplicate`. Those solution finding difficulties, so please help me. – user4948798 Apr 07 '22 at 08:22
  • I've voted for `reopen`. I'll try to ask someone else for another one. Please wait for a while. – tshiono Apr 07 '22 at 08:34
  • The question has been happily reopened. I've just posted my answer. Hope it helps. – tshiono Apr 07 '22 at 09:02

1 Answers1

1

Would you please try the bash script:

#!/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

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.
tshiono
  • 21,248
  • 2
  • 14
  • 22
  • First of all thanks for assisting me on this. Script provides output as `b16e145bac197a36802a31c5886ad726ee4f38c4 member/brazil/linux/team ef02b22ac4ce179a0064b1df2b326fd6b5dce514 false member/brazil/linux/team` it is not replacing the UUID with new one, instead it is appending. Actually if i run `ssh -p 29418 review.example.com gerrit ls-groups -v | awk '-F\t' '$1 == "member/brazil/linux/team" {print $2}'` along with `awk` only it prints only UUID. – user4948798 Apr 07 '22 at 09:43
  • Thank you for the feedback. Can you provide the outout of the command `review.example.com gerrit ls-groups -v` as a file? Redirect the command output to a file and post the result in your question. If some file uploading service is available, it will be much better because it will preserve tab characters and other special characters. BR. – tshiono Apr 07 '22 at 10:26
  • Thank you for the prompt update. I had misunderstood the output of `ssh` has only two fields a line. Now the code is fixed by appending the third variable name `_` after `group` and `uuid` in the `read` builtin. The variable `_` is assigned to the third and remaining fields absorbing the unused fields. – tshiono Apr 07 '22 at 10:47
  • How do i write the output in `groups` file? – user4948798 Apr 07 '22 at 11:24
  • Redirect the output to `newgroups` file by adding " > newgroups" to the last line of the script (I have updated my answer accordingly). If the file looks correct, rename it with `mv -i newgroups groups`. Backup the original `groups` file in advance. – tshiono Apr 07 '22 at 11:35