LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can I improve this AWK/SED script that lists all users in columns? (https://www.linuxquestions.org/questions/programming-9/can-i-improve-this-awk-sed-script-that-lists-all-users-in-columns-897877/)

hgate73 08-17-2011 11:54 AM

Can I improve this AWK/SED script that lists all users in columns?
 
Hi there,

Building a user management script (add/delete/lock/unlock/list/etc users) and building my line to pull all usernames from /etc/passwd and display ONLY user names (no machine names, no special names) and I am pretty close. I'd like to improve it though.

Here's the line currently:
Code:

awk -F":" '$7 ~ /\/bin\/bash/ {print $1}' /etc/passwd | sed '/\$/d' | sort | uniq -u | column
Output
Code:

user1                  user2                user3                  user4
Can I improve the one-liner?

ta0kira 08-17-2011 12:39 PM

Quote:

Originally Posted by hgate73 (Post 4445730)
Hi there,

Building a user management script (add/delete/lock/unlock/list/etc users) and building my line to pull all usernames from /etc/passwd and display ONLY user names (no machine names, no special names) and I am pretty close. I'd like to improve it though.

Here's the line currently:
Code:

awk -F":" '$7 ~ /\/bin\/bash/ {print $1}' /etc/passwd | sed '/\$/d' | sort | uniq -u | column
Output
Code:

user1                  user2                user3                  user4
Can I improve the one-liner?

Why don't you use sort -u here? This isn't an issue given your input data, but sort | uniq -u will keep only lines that have no duplicates. You might also compare the login shells to /etc/shells rather than assuming regular users use bash.
Kevin Barry

edit:
Code:

grep -f /etc/shells /etc/passwd | cut -d: -f1 | sort | column

hgate73 08-17-2011 12:56 PM

Quote:

Originally Posted by ta0kira (Post 4445768)
Why don't you use sort -u here? This isn't an issue given your input data, but sort | uniq -u will keep only lines that have no duplicates. You might also compare the login shells to /etc/shells rather than assuming regular users use bash.
Kevin Barry

edit:
Code:

grep -f /etc/shells /etc/passwd | cut -d: -f1 | sort | column

Hi ta0kira,

Will uniq -u ignore lines that have duplicates (that is, if a line has a duplicate, remove BOTH lines)? My intention is to list, with no duplications, every user on the system (minus machine names, which for us contain a "$" sign).

AFAIK all our users use Bash, but if it would help make the script more portable I can use /etc/shells instead.

ta0kira 08-17-2011 01:17 PM

Quote:

Originally Posted by hgate73 (Post 4445789)
Will uniq -u ignore lines that have duplicates (that is, if a line has a duplicate, remove BOTH lines)?

Yes, it will remove both lines. In other words, only lines occurring once (consecutively) will be printed. I think a duplicate username in /etc/passwd would be an error you'd want to see and fix rather than mask.
Quote:

Originally Posted by hgate73 (Post 4445789)
AFAIK all our users use Bash, but if it would help make the script more portable I can use /etc/shells instead.

I'm not sure if all *nix systems use /etc/shells, but I would use it in case someone later on wants their shell set to something else.
Kevin Barry

hgate73 08-17-2011 01:19 PM

Perfect, thanks for explaining.

I've updated the line to:
Code:

grep -f /etc/shells /etc/passwd | cut -d: -f1 | sed '/\$/d' | sort | column


All times are GMT -5. The time now is 06:16 PM.