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.
About this post
- Posted
- Archived
- Edited
- Score
- 3 · 100% up
- Kind
- Text
- ID
- mx3sze
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 withwc.And if I run the systemctl command and pipe it into grep without
-c, I can see several lines are missing from the output.Deleted before we saved it.
Oh of course! Hahaha
Thank you
Count them yourself and see what the difference is
wc -lseems to be counting the number of lines generated by systemctl, withgrep -c -seemingly counting some quantity I can't figure outWhat do you think is the pattern you feed to grep to search for?
Have you differ the output
How do you mean? They are being piped identical inputs