LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 08-17-2011, 11:54 AM   #1
hgate73
LQ Newbie
 
Registered: Sep 2008
Posts: 17

Rep: Reputation: 0
Thumbs up 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
 
Old 08-17-2011, 12:39 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by hgate73 View Post
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.
 
Old 08-17-2011, 12:56 PM   #3
hgate73
LQ Newbie
 
Registered: Sep 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ta0kira View Post
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.
 
Old 08-17-2011, 01:17 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by hgate73 View Post
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 View Post
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.
Old 08-17-2011, 01:19 PM   #5
hgate73
LQ Newbie
 
Registered: Sep 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Perfect, thanks for explaining.

I've updated the line to:
Code:
grep -f /etc/shells /etc/passwd | cut -d: -f1 | sed '/\$/d' | sort | column
 
  


Reply

Tags
awk, passwd, sed, users


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert column with awk or sed between two columns captainentropy Linux - Newbie 8 01-19-2011 11:03 PM
awk and sed help for script technocp Programming 3 05-16-2010 12:14 AM
awk sed shell script slcalice Programming 3 03-09-2009 05:40 AM
Moving columns with sed or awk? btm Linux - Newbie 4 09-27-2007 02:03 PM
sed script to read only columns 4 to 6 in output database cranium2004 Programming 10 02-28-2006 07:20 AM


All times are GMT -5. The time now is 09:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration