Thank you, but it should not list
root:x:0:0:root:/root:/bin/bash
and also, should list
baduser3:x:500:500:root:root:/bin/bash
as baduser3 has home directory set as /root
You said you want to use sed or awk, cut (as suggested by rbatte1) is another possibility. I would guess that awk will be better for this job than sed.
You've been watching what we have been doing here for more than 1.5 years.
Can you show us how to use awk (or grep and cut) to get the UID, GID, and home directory for root out of /etc/passwd?
Once you have those values, can you show us how to use awk or cut to find other users with the same UID as root, the same GID as root, or the same home directory as root?
Show us that you have made an attempt to solve this problem by showing us what you have tried so far. Tell us what is working and what you can't get to work yet.
Note that your output doesn't include the login names of the users whose entries are failing your tests. You might want to add that to your output.
If you'd like to get root's UID, GID, and login directory from the password file instead of assuming that they will be 0, 0, and /root, respectively, (and you're willing to live with the limitation that root's information must appear on the 1st line in the passwd file), you could consider something like:
#!/bin/ksh
awk -F: '
NR == 1 {if($1 != "root") {
printf("root expected on first line in %s; found %s instead.\n",
FILENAME, $1)
exit 1
}
rootuid = $3
rootgid = $4
roothd = $6
next
}
$3 == rootuid || $4 == rootgid || $6 == roothd { print }' "${1:-testpasswd}"
Note also that baduser3's login directory ( root ) is not identical to root's login directory ( /root ), so I'm not sure if that line should be included in your output or not. (However, all login directories in /etc/passwd should have absolute pathnames. Should you add a check to verify that each user's login directory starts with a / ?)
If you don't want to print the entire line from the passwd file for lines that fail your tests, you've already shown that you know how to print just the fields you want.