Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2452
  • Last Modified:

USING WC and GREP together

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
EaglesFan
Asked:
EaglesFan
  • 3
  • 2
  • 2
  • +3
1 Solution
 
ozoCommented:
grep "^[^:]*:[^:]*:[^:]*:$group:" /etc/passwd | wc
0
 
gothickCommented:
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
 
bchewCommented:
cut -d: -f3 /etc/passwd | grep <group number> | wc -l
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
n0thingCommented:
awk -F: '{print $4}' /etc/passwd | grep <groupid> | wc -l
is another solution
0
 
ozoCommented:
#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
 
bchewCommented:
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
 
kellyjjCommented:
grep "<group id>" /etc/passwd |wc -l
0
 
bchewCommented:
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
 
braveheartCommented:
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
 
braveheartCommented:
Take it easy.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now