4 ms·
Useless use of `cat`. :) wc -l file To hide filename: wc -l < file
by dyanaraps 7y ago
Useless use of `cat`. :)
wc -l file
To hide filename:
wc -l < file
- scarejunba 7y agoI'm actually pretty much against UUoC. The cat example is much more resilient to modification. For instance, there are usually natural modifications considering filtering etc. Anyway, 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.
- navaati 7y agoThat's right regarding directly giving the filename for the program (here wc) to open itself. 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
- thestoicattack 7y agoUseless use of `wc` :) <file grep -c meh
- deleted 7y ago[deleted]
- herpderperator 7y agoI am confused. What's wrong with `wc -l < file | ... | ...`? I 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?
- emj 7y agoYou can easily change that first command with muscle memory. That's why I use cat $file| but I guess <$file would work too.
- emmelaich 7y agoAs per James Taylor's comment above, using cat means you can easily replace it with head e.g. for testing. And not change anything else in the pipeline.
- squiggleblaz 7y agoBecause I feel the other answers, though correct, are entirely missing out on clarity: The first iteration looks like this: <file head -n10 | grep foo | tr baz qux The second iteration looks like this: <file gzcat | head -n10 | grep foo | tr baz qux It makes the change much easier, since the first thing written is the first thing that happens. In `gzcat < file`, the logical first step - reading and streaming file - is now the physical second step. Like a German sentence, whenever you want to prepend the file, you have to maintain an item in second position.
- scarejunba 7y ago25 years using shells and I never thought to place input redirection at the front. Well, thank you!
- deleted 7y ago[deleted]
- james_s_tayler 7y agoI hate when people nitpick this stuff. What if we just straight prefer the first one?
- BeetleB 7y agoThere's a whole Wikipedia section for this: https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat
- james_s_tayler 7y agoIt all makes a lot more sense now. I'm glad I can add demoggification to my vocabulary.
- calvinmorrison 7y agoWell one reason is programs being smart (dumb) about output if they think that dev stdout is a dev console. Using an extra cat eliminates this.
- mkl 7y agoI usually use cat, because I often build a pipeline by starting 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.
- Izkata 7y agoLikewise here, it maintains the left-to-right direction and "one operation per | gap |"
- tomp 7y agoBad code is code smell. 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).
- shermanyo 7y agoor just: cat -n file
- messe 7y agoOr even: nl file