by jrockway · 16y ago · 0 comments
cat file | xargs foo | grep bar | sort | wc -l
It just looks nicer than: < file xargs foo | grep bar ...You failed at that?
I'm intrigued because it always occured to me that you were a particularly fastidious person (with all due respect).
I often come accross issues that would seem for all the world to be a pointless artifact of an accepted convention. If this accepted convention is an acquired trait and one learns exclusivly from a limited meduim (irc frustrates me), I find it very curious that they persist as they sometimes act as a difficult barrier for a thorough understanding. This xargs thing, though I know almost nothing about it, seems to parallel similar problems I've encountered and I for one would be interested to hear your take on this (and HN in general).
Or just some food for thought for your next blog post maybe?
The point was, when I used "cat file |", I knew it was going to work, and it did. When I tried to eliminate the cat by using the program's built-in ability to read a file, I had to read several manpages before determining that it was not possible. All because some tutorial says "cat is useless", when it clearly saved me much more time than the extra CPU time it used.
And if you are actually asking; xargs is just a utility to read command-line-arguments from stdin. `echo -e "foo\nbar" | xargs rm' === `rm foo; rm bar' (or depending on the xargs implementation; `rm foo bar'). It kind of reminds me of a "functor map" operation, where stdin is a functor (of command-line arguments), and command-line programs are functions. (I will now mention that xargs also does "join" on the "results" of the "function", which is very ... monad-like. But "Monads are teh awesome and everything is one" is my second-least-favorite Internet meme, so I will spare you. :)
I tried to figure out how to make xargs read from a file
instead of stdin, and was unsuccessful.
"xargs -a <filename>" First option described in the man page on Red Hat. (xargs foo | grep bar | sort | wc -l) < file
It makes the pipeline one command, both lexically (verb comes first) and concretely (the pipeline is kicked off in a subshell) verb file
cat file | verb1 | verb2 | verb3
(verb1 | verb2 | verb3) < file
verb123 file
EDIT: Consider the calling conventions, which one of these handles it's arguments differently? Assume that verb123 is an equivalent to the pipeline -- the subshell+stdin construction lends itself to shell aliases: alias verb123="(verb1 | verb2 | verb3) < "Of course, with the usual argument vs stdin conventions, either of the middle two could also be rewritten:
verb1 file | verb2 | verb3
This is probably how I'd write such a chain. (wc Lines) . sort . (grep "bar") . (xargs "foo") =<< file
But note in this case that all the data "flows in the same direction". wc Lines $ sort $ grep "bar" $ xargs "foo" =<< file Prelude> :info $
($) :: (a -> b) -> a -> b -- Defined in GHC.Base
infixr 0 $
Prelude> :info =<<
(=<<) :: (Monad m) => (a -> m b) -> m a -> m b
-- Defined in Control.Monad
infixr 1 =<<
Prelude> :info .
(.) :: (b -> c) -> (a -> b) -> a -> c -- Defined in GHC.Base
infixr 9 .
Incidentally, the parens in my example are actually unnecessary. Function application is about 10, and (.) is 9.It's too easy to make a mistake and type >
..and that can ruin your whole day.
I find that to be a far more pernicious design error -- they should have made the longer token the destructive one, or used another character in it.
The point is a logical improvement anyway (not burying the input argument near the beginning). I'm kind of surprised that the bash folks haven't turned cat into a builtin like they did with time and some of the other coreutils.
It's too bad it's about 30 years too late to stem the tide of shit like cat -v: http://harmful.cat-v.org/cat-v/
xargs foo < file | grep bar | sort | wc -lThese days, if you hit a performance wall from spawning too many cats, I would think you switch to some scripting language where you have everything in one process. Premature optimization, people...
cat file.txt | sort | uniq | wc -l ## notice uniq
it gives you the count of unique lines. If you omit the sort in that instance it will fold duplicates together a/b/a is three lines, not two.
cat file | xargs foo | grep bar | sort | wc -l