Useless use of …
(Inaugurating a new category which will be kind of my own dailty wtf)
A pretty well known matter is how some tools can be abused. Usually, it is cat
or grep
. Typical examples include the following two:
cat foo | grep bar
grep bar foo | awk '{print $2}'
Obviously, the first one can be reduced to:
grep bar foo
and the second, to:
awk '/bar/ {print $2}' foo
awk
and sed
are often part of the reason grep
gets abused, because a lot of people don't know the latter form above or its sed
equivalent. They don't know they could restrict the match on a specific column, too, which could be important in some cases.
Anyways, a few months ago, I saw the most beautiful short piece of combined useless use of something I've ever seen, which also happens to be a pretty unusual type:
more $file | wc | awk '{print $1}'
Not only does it use more
where even cat
, while still being useless, would be a better choice, but it also manages to use awk
where wc
could take an option to achieve the same goal.
Yes, the above can be simplified as:
wc -l < $file
We never found who was responsible for this nicety.
2008-12-20 11:17:51+0900
Both comments and pings are currently closed.
2008-12-20 13:49:52+0900
cat file | pipe-construction is not always totally useless. When iteratively building a piped set of commands, you may do something like head some-file | try-some-stuff to ensure you’ve matched on the right columns, etc.; then when you are happy it works you do something like ^head^cat; or even manually edit the line and insert cat at the front, which may be quicker than reworking the line to avoid the ‘useless’ invocation.
2008-12-20 15:50:33+0900
The other reason you might want the “cat file |” bit is that this way, stdin is a pipe rather than a file. Some programs behave differently when stdin is not seekable.
2008-12-20 18:25:41+0900
actually wc is one of them. Hence the two commands you mentioned are not exactly the same:
$ wc -l test_doc
0 test_doc
$ wc -l < test_doc 0
2008-12-21 02:34:17+0900
more $file | wc | awk ‘{print $1}’ is not the same as wc -l $file, it is the same as “wc -l < $file"
2008-12-21 02:36:05+0900
Your Blog has problems with the less-than symbol in comments. It just ignores everything after it.
2008-12-21 14:09:58+0900
Julian: Actually, the less-than symbol was supposed to be in my post, but somehow didn’t get there. Seems like the comment system also has problems.
2008-12-21 18:01:46+0900
Wow! I am guilty of some of these.
Please keep this series up!
2009-01-05 21:29:12+0900
Talking about “wc”…
If you want to know the size of a file in bytes, “wc -c foo.bar” is the best choice, because it will be smart enough to not read the whole file, but instead will seek directly to the end, or get the size from file stats. In other words, “wc -c” is fast, while “wc” isn’t.
Other option is “find -printf ‘%s %p\n'”