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
............
............................
CodePudding user response:
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
sshcommand in thewhileloop, splitting the line into fields and assigning two variablesgroupanduuidto the 1st and 2nd fields. Then an arrayaryis assigned touuidindexed bygroup. If the output ofsshcontains multiple lines (multiple group-uuid pairs), the array holds the values as many. - The output of
sshis fed to thewhileloop via theprocess substitutionmechanism with the syntax< <(command). - The second
whileloop processes the filegroupsreplacing theuuidassociated withgroup, which is assigned in the first loop. - The original
groupsfile is not overwritten. The output is redirected to a new filenewgroups. If the file looks correct, rename it withmv -i newgroups groups. Backup the originalgroupsfile in advance.
