1

I have two files in style of /etc/group :

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:username
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
ubuntu:x:999:

And I want to merge them into a third file, but no GID should occur twice in the merged file. How could this be done using bash command? I was thinking that it could be done with awk, but I cannot find a solution at the moment.

EDIT I need to keep the original GID from each file there could be two groups with same name, but not with same GID. in the output file the GID should be unique `

  • Do you want to also merge the user lists if the same group appears in both files? Same by name or same by gid? – ilkkachu Jan 25 '18 at 21:37
  • @ilkkachu Yes, there can be two groups with same name, but not with same GID. in the output file the GID shoudl be unique – AndrewD Jan 25 '18 at 21:40
0
awk -F: 'BEGIN { OFS=":" } { print $1,"x",NR,$5 }' file1 file2 file3
  • I need to keep the original GID from file1 file2 – AndrewD Jan 25 '18 at 21:29
  • If you have a collision, which one do you keep? – DopeGhoti Jan 25 '18 at 21:30
  • The first one should be kept – AndrewD Jan 25 '18 at 21:32
0

This will select the first name and number from both files:

awk -F: -vOFS=":" '{if(!($1 in gname || $3 in gnumber)){print $1,"x",$3,$4};gname[$1]=1;gnumber[$3]=1}' file1 file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.