Solved

USING WC and GREP together

Posted on 1998-11-15
10
2,411 Views
Last Modified: 2013-12-26
I need to use the "GREP" and "WC" commands on a single line to find out how many accounts are in the same group   /etc/passwd (My own server)...what is the proper syntax to use?
0
Comment
Question by:EaglesFan
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1294156
grep "^[^:]*:[^:]*:[^:]*:$group:" /etc/passwd | wc
0
 
LVL 2

Expert Comment

by:gothick
ID: 1294157
You can use the "cut" command as an alternative to that grep syntax -- it can cut out
the group column more easily, then you can pass that to grep/wc.
0
 
LVL 5

Expert Comment

by:bchew
ID: 1294158
cut -d: -f3 /etc/passwd | grep <group number> | wc -l
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:n0thing
ID: 1294159
awk -F: '{print $4}' /etc/passwd | grep <groupid> | wc -l
is another solution
0
 
LVL 84

Expert Comment

by:ozo
ID: 1294160
#the above solutions with cut and awk probably want to use grep -x
#or, using neither grep nor wc:
perl -F: -ane '$c+=$F[3]=='«groupid»';END{print "$c\n"}' /etc/passwd
0
 
LVL 5

Expert Comment

by:bchew
ID: 1294161
ozo is right about grep -x.  Otherwise, you will be counting groups you didn't intend.  For instance, if you grep for 100 without the -x you would also get groups 1000, 1100, 2100, etc.

0
 
LVL 2

Accepted Solution

by:
kellyjj earned 50 total points
ID: 1294162
grep "<group id>" /etc/passwd |wc -l
0
 
LVL 5

Expert Comment

by:bchew
ID: 1294163
kellyjj

First of all, your "answer" won't work (it will give lots of false hits)!  Furthermore, what you propose is as an has already presented by others as comments!  I believe this is a blatant attempt to "steal" unearned points rather than try to help someone with a solution to their problem.
0
 
LVL 3

Expert Comment

by:braveheart
ID: 1294164
I reckon you should use nawk:

nawk -F: '{groups[$4]++}\\
END      {for (grp in groups) print grp, groups[grp]}' \
/etc/passwd

This will list two columns - the first contains the number of occurences of the group and the second contains the group id.
0
 
LVL 3

Expert Comment

by:braveheart
ID: 1294165
Take it easy.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now