Question

I want to cut the lines of /etc/passwd of users of the same group.

If my group have the uid 1009 I want all the lines of /etc/passwd of this group

user1:x:1001:1009::/home/user1:/bin/bash
user2:x:1002:1009::/home/user2:/bin/bash
user3:x:1003:1009::/home/user3:/bin/bash

I tried cat /etc/passwd | grep 1009 but it doesn't work because the number 1009 can be also a uid or other number.

Was it helpful?

Solution

Use awk to compare the forth field against the uid you want using the field separator :

$ awk -F: '$4==1009' /etc/passwd
user1:x:1001:1009::/home/user1:/bin/bash
user2:x:1002:1009::/home/user2:/bin/bash
user3:x:1003:1009::/home/user3:/bin/bash

OTHER TIPS

cat /etc/passwd | egrep '([^:]+:){3}1009:'

"Three lots of (some not-colons then a colon), followed by 1009 then another colon."

(You need the trailing colon to omit group 10090.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top