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

wtf

Both comments and pings are currently closed.

8 Responses to “Useless use of …”

  1. Jon Says:

    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.

  2. Simon Says:

    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.

  3. Tzafrir Cohen Says:

    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

  4. Julian Andres Klode Says:

    more $file | wc | awk ‘{print $1}’ is not the same as wc -l $file, it is the same as “wc -l < $file"

  5. Julian Andres Klode Says:

    Your Blog has problems with the less-than symbol in comments. It just ignores everything after it.

  6. glandium Says:

    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.

  7. ushimitsudoki Says:

    Wow! I am guilty of some of these.

    Please keep this series up!

  8. Denilson Says:

    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'”