LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
Search this Thread
Old 08-13-2008, 03:02 PM   #1
leebrent
Member
 
Registered: Oct 2007
Location: Nanaimo
Distribution: Red Hat 5
Posts: 39

Rep: Reputation: 15
Directory Related Question


I have a regular expression question. Currently, I am running the following command to get a list of all users on my box. How can I modify the following script to exclude users: group1, group2, group3.... I know I need to pipe it into grep, but I am unsure if this is a good approach?

Code:
ls -l /home
 
Old 08-13-2008, 03:12 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
You can do this in a number of ways.

But first, let's be clear about the filtering criteria. In the output of "ls -l", you have fields for file owner and for the group. Which field do you want to act on? The best thing is to post a sample output and tell us which lines you would like saved. Also, do you want to whole content of ls -l, or just the file name?
 
Old 08-13-2008, 04:06 PM   #3
leebrent
Member
 
Registered: Oct 2007
Location: Nanaimo
Distribution: Red Hat 5
Posts: 39

Original Poster
Rep: Reputation: 15
Post

The number of fields I get back from ls -l, I am throwing into a php array with exec(), and then taking what I need from the array to write a listing. If it is possible I only need user and group.

Current Output:

drwxrwx--x 19 user1 user1 4096 Jul 3 2007 user1
drwxrwx--x 6 user2 user2 4096 May 8 22:56 user2
drwxrwx--x 4 fname1.lname1 fname1.lname1 4096 Jul 10 20:42 fname1.lname1
drwxrwx--x 4 group1 group1 4096 Oct 23 2007 group1
drwxrwx--x 3 group14 group14 4096 Oct 23 2007 group14
drwxrwx--x 3 group15 group15 4096 Oct 23 2007 group15
drwxrwx--x 3 group16 group16 4096 Oct 23 2007 group16
drwxrwx--x 3 group17 group17 4096 Oct 23 2007 group17
drwxrwx--x 3 group18 group18 4096 Oct 23 2007 group18
drwxrwx--x 3 group19 group19 4096 Oct 23 2007 group19

Our conventions changed about a year ago, so I have a mix of users on the system, some with the fname.lname and some with finitiallastname.

I would love my output to look like this:

user1 user1
user2 user2
fname1.lname1
fname2.lname2

I want to exclude the user's that contain the word group from the output.
 
Old 08-13-2008, 06:42 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If all of your "real user" IDs are greater than 999, try this:
Code:
egrep '^[^:]+:[^:]+:[0-9]{4}' /etc/passwd | sed -r 's/^([^:]+):.*$/\1/' | while read user; do
  echo $user $(id -ng $user)
done
ta0kira

Last edited by ta0kira; 08-13-2008 at 06:44 PM.
 
Old 08-13-2008, 07:23 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
ls -l|grep -v group|awk '{print $3 " " $4}'

note that in your example output you've got the username and group for the 1st 2 lines, but only the username for the 2nd 2 lines. Seems inconsistent.
 
Old 08-13-2008, 07:45 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
ls -l|grep -v group|awk '{print $3 " " $4}'

note that in your example output you've got the username and group for the 1st 2 lines, but only the username for the 2nd 2 lines. Seems inconsistent.
What is grep -v group for? Do you sub something in for group?
ta0kira

edit:
Nevermind. Stupid question now that I look again. I was thinking it was supposed to be universal.
 
Old 08-14-2008, 01:28 AM   #7
leebrent
Member
 
Registered: Oct 2007
Location: Nanaimo
Distribution: Red Hat 5
Posts: 39

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
ls -l|grep -v group|awk '{print $3 " " $4}'

note that in your example output you've got the username and group for the 1st 2 lines, but only the username for the 2nd 2 lines. Seems inconsistent.

Hey Chris,

It is an editted post. I did not want to post student data in my post... I will post one with users who are fake to make it easier to work with. I am trying to get only the users who do NOT have group within their username.

Code:
drwxrwx--x 19 desrochersm        desrochersm        4096 Jul  3  2007 desrochersm
drwxrwx--x  6 doddg              doddg              4096 May  8 22:56 doddg
drwxrwx--x 11 dutchukm           dutchukm           4096 May  4 18:44 dutchukm
drwxrwx--x  4 eric.gilmer        eric.gilmer        4096 Jul 10 20:42 eric.gilmer
-rwxrwx--x  1 root               root                  0 Jul  4  2007 exit
drwxrwx--x  6 farmerer           farmerer           4096 Jul  3  2007 farmerer
drwxrwx--x  4 group1             group1             4096 Oct 23  2007 group1
drwxrwx--x  3 group10            group10            4096 Oct 23  2007 group10
drwxrwx--x  3 group11            group11            4096 Oct 23  2007 group11
So I would want the following out of this from the regular expression:

root
dutchukm
desrochersm
doddg
farmerer

Can I grep and only show things that do not contain group (this is within the username, not actual group)?

Like ls -l /home | grep --someswitch "group"?

Cheers,

Brent

Last edited by leebrent; 08-14-2008 at 01:29 AM.
 
Old 08-14-2008, 03:12 AM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
My point was some of your example reqd output had username & groupname, some only had username.
If you only want usernames output and only want to check usernames, then

ls -l|awk '{print $3}' |grep -v group

ie list files (long format), grab only usernames, ignore usernames containing string 'group'

BTW, usually in Linux ls -l is aliased to ll, so I'd use

ll|awk '{print $3}' |grep -v group

for conciseness
 
Old 08-14-2008, 04:17 AM   #9
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Code:
awk '!/group/ { print $3; }'
But since the original goal was to get a list of users, the approach of ta0kira seems to be more suitable.
 
Old 08-14-2008, 02:33 PM   #10
leebrent
Member
 
Registered: Oct 2007
Location: Nanaimo
Distribution: Red Hat 5
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks Everyone,

Your solutions worked like a charm, I ended up using:

ls -l /home | awk '!/group/ {print $3; }'

Now to make php like it

Cheers,

Brent
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
c related question. blackzone Programming 1 07-24-2004 09:55 AM
bash related question MattSmith Linux - General 1 01-20-2004 06:41 AM
DNS related question tusher Linux - Newbie 1 12-07-2003 02:03 PM
Vmware Related Question jon_k Linux - Software 2 07-22-2003 07:16 PM
WEBCAM related question aznowicz Linux - Software 2 08-18-2002 01:36 PM


All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
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