by rhinoceraptor · 7y ago · 0 comments
Ah, simple :)
cat file | wc -l wc -l file
To hide filename: wc -l < fileAnyway, since he said number we probably want `nl` not `wc`.
Separating pipe-setup from processing is sound engineering to me and makes things much easier. For instance as I iterate, I will have cat piped to head piped to filter but eventually I'll take the head out and run the whole thing. That's a trivial ^W versus moving around arguments and worrying about arg order, etc.
However using the input redirect is fine, especially if you format it that way (which is exactly the same thing):
<file wc -l
Then, adding another step is as natural as with cat: <file grep meh |wc -l <file grep -c mehI know `< file wc -l | ... | ...` is identical because the redirection is done before the command is executed, but what does putting it in front help with?
head filename | ...
then just change "head" to "cat" when I get it working, or grep if I want to check parts of a file, etc.In this particular case, people (like me) would wonder, "what's the point" and then go searching for the details of how `cat` and `wc` work, assuming that there's might be a reason the original developer wrote this code, as opposed to the simpler `wc -l` (i.e. Chesterton's Fence).
Also, context is really important. Not that people on the internet ever stop to think about what the context is that the person is writing the code in. I don't write bash scripts that run mission critical workloads. I just ad-hoc type stuff into my shell to produce output I need for stuff at the time.