4 ms·
Exactly. Things like the "useless use of wc -l" fall into a similar category for me. When I want to count the lines of stdout, I use "wc -l". It may not be the
by mjb 13y ago
Exactly. Things like the "useless use of wc -l" fall into a similar category for me. When I want to count the lines of stdout, I use "wc -l". It may not be the simplest, most efficient or fewest keystrokes, but it is extremely easy to remember and fits well into the model of a pipeline of simple single-purpose tools. I tend to use cat at the beginning of pipelines just because it keeps the flow left-to-right, and makes it much clearer what the source of the data is.
There's value in knowing some of the advanced things that the textutils and bash can do together, and sometimes there's value in using those things. I don't dispute that, but I do dispute the need to shame people who use the tools in ways that you personally don't like.
Shame or not, I'm going to keep using cat at the beginning of my pipelines unless I have a really good reason not to. If that makes some people think I'm not in the Unix wizard in-group, I don't really care.
- sliverstorm 13y agoPipes also match my thought flow. "wc -l" is a good example. "Ok, I want to read this file..." cat file "Then I want to find foo" cat file | grep foo "Oh, and I want to know how many lines of foo there are" cat file | grep foo | wc -l In this trivial example, I could have inserted "-c" with only a few keystrokes, but on a 200 character string of commands it's faster and easier to tack "wc -l" on at the end. Plus I'm not strong enough with grep to do every manipulation I might want, so sometimes that grep will be followed by some perl, or a sort, etc.
- mindcrime 13y agoFWIW, that's the exact same way I approach that sort of manipulation. Start with the most basic operation, then construct a pipeline going left to right, adding filters / operations as needed to get to the result.
- scjody 13y agoIf you're doing a one-off pipeline on the command line, you can even start with less! "I want to look through this file..." less file "Oh, that's long, I want to look at all the foos..." less file | grep foo "How many foos are there?" less file | grep foo | wc -l Try it, it works!
- gizmo686 13y agoThis is not some magic behavior of pipelines. Rather, less (like many other terminal programs) is explicitly programmed to behave differently if the output is a terminal. Specifically, the less source code has this comment in it: "/* * Output is not a tty. * Just copy the input file(s) to output. */" grep does this to, which is why the color-coding control characters don't mess up other commands.