Skip to content

Why do grep and wc give different results when counting services?

~ > systemctl -t service —state=active —no-pager —no-legend | grep -c -
35
~ > systemctl -t service —state=active —no-pager —no-legend | wc -l
55

EDIT: wc -l seems to be counting the number of lines generated by systemctl, with grep -c - seemingly counting some quantity I can't figure out

SOLVED:

You can't run grep without a pattern. grep -c - is counting lines that have a - in them, that's not the input file parameter. For it to do what the OP is thinking it's doing you need to pass an empty pattern like grep -c "" -

Edited 23 Apr 2021. We only hold this version.

3 points10 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
3 · 100% up
Kind
Text
ID
mx3sze

Author

28
posts
111
comments
9
media
View profile

10 comments

I don't know what grep's default behavior is when being run without a pattern, but if I do the same thing with grep -c ".*" - instead it shows the same results as with wc.

And if I run the systemctl command and pipe it into grep without -c, I can see several lines are missing from the output.

2 points

Count them yourself and see what the difference is

1 points

wc -l seems to be counting the number of lines generated by systemctl, with grep -c - seemingly counting some quantity I can't figure out

2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.