LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 09:08 AM   #1
a2326
Member
 
Registered: Oct 2012
Distribution: Debian
Posts: 49

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 09:27 AM.
 
Old 11-16-2012, 09:30 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 09:48 AM   #3
a2326
Member
 
Registered: Oct 2012
Distribution: Debian
Posts: 49

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, 09:57 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration