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
 
Search this Thread
Old 11-16-2012, 10:08 AM   #1
a2326
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Rep: Reputation: Disabled
Extracting all group members


Hello,
I want to extract all members of a group, but I don't get the output that I want:
Code:
GROUP=groupname
cat /etc/group | grep --regex "^"$GROUP":.*" | awk -F:, '{print $4}'
I would like to have an output with one user per line like:
username1
username3
....


Is it good practice in shell script to save such data in a file and then read the file when the data shall be used or are there better techniques? Thanks.

Last edited by a2326; 11-16-2012 at 10:27 AM.
 
Old 11-16-2012, 10:30 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by a2326 View Post
Hello,
I want to extract all members of a group, but I don't get the output that I want:
Code:
GROUP=groupname
cat /etc/group | grep --regex "^"$GROUP":.*" | awk -F:, '{print $4}'
I would like to have an output with one user per line like:
username1
username3
....
Have a look at this:
Code:
awk -F: '/^'$GROUP'/ { gsub(/,/,"\n",$4) ; print $4 }' /etc/group
Quote:
Is it good practice in shell script to save such data in a file and then read the file when the data shall be used or are there better techniques? Thanks.
I'm not 100% sure what it is you are asking. If you are talking about saving the output of one command to use it as input for a second command then the answers is: No, try to avoid this if possible.
 
2 members found this post helpful.
Old 11-16-2012, 10:48 AM   #3
a2326
LQ Newbie
 
Registered: Oct 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
That works, thank you :-) To be more precise: Is it a good choice to store the extracted data set in a file or is better to store the data in some kind of shell script data structure? E.g., in Java or C++ you often store file data in arrays or lists and then you just loop through this data structure. However, transferring Java way of thinking to shell scripting is often not possible that's why I am asking this question.
 
Old 11-16-2012, 10:57 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by a2326 View Post
To be more precise: Is it a good choice to store the extracted data set in a file or is better to store the data in some kind of shell script data structure? E.g., in Java or C++ you often store file data in arrays or lists and then you just loop through this data structure. However, transferring Java way of thinking to shell scripting is often not possible that's why I am asking this question.
That all depends on the data and what it is you need/want to do with it (same is probably true for higher programming languages).

Bash, awk and perl (to name just 3) can use array's if needed. Do check the specific documentation, I'm sure that array's are handled differently then in Java or C(++).

These might help:

Bash resources:
Sed/Awk resources:
 
1 members found this post helpful.
  


Reply

Tags
awk, grep, shell script


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
FTP allow only members of group markdjones82 Linux - Server 1 12-16-2009 08:22 PM
Locate Members of a Group? carlosinfl Linux - General 6 09-05-2008 03:26 PM
Listing the members of a group armandino Linux - General 5 06-24-2007 07:43 PM
all winbind members of same group paul_mat Linux - Networking 0 01-16-2006 06:46 PM
winbind: group members? eantoranz Linux - Software 0 11-01-2005 09:47 AM


All times are GMT -5. The time now is 05:09 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