Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-17-2011, 11:54 AM
|
#1
|
|
LQ Newbie
Registered: Sep 2008
Posts: 17
Rep:
|
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?
Last edited by hgate73; 08-17-2011 at 11:56 AM.
Reason: clarification
|
|
|
|
08-17-2011, 12:39 PM
|
#2
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,966
Rep: 
|
Quote:
Originally Posted by hgate73
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
Last edited by ta0kira; 08-17-2011 at 12:44 PM.
|
|
|
|
08-17-2011, 12:56 PM
|
#3
|
|
LQ Newbie
Registered: Sep 2008
Posts: 17
Original Poster
Rep:
|
Quote:
Originally Posted by ta0kira
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.
|
|
|
|
08-17-2011, 01:17 PM
|
#4
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,966
Rep: 
|
Quote:
Originally Posted by hgate73
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
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
|
|
|
1 members found this post helpful.
|
08-17-2011, 01:19 PM
|
#5
|
|
LQ Newbie
Registered: Sep 2008
Posts: 17
Original Poster
Rep:
|
Perfect, thanks for explaining.
I've updated the line to:
Code:
grep -f /etc/shells /etc/passwd | cut -d: -f1 | sed '/\$/d' | sort | column
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:12 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|