Advantages of cat'ing file and piping to grep
Better to avoid cat; write it this way if line editing matters:
$ < filename grep pattern
The reason is that pushing all the data through cat costs memory and CPU resources. Another benefit of passing the filename as an argument rather than redirect stdin is that it allows the command the option to mmap() the file.
I can't believe no one has referenced "Useless Use of Cat" http://www.smallo.ruhr.de/award.html yet
There is one questionable advantage. If you have a long pipeline, it looks a bit more orthogonal with cat:
cat file | command1 | command 2 | command3
It clusters all the commands together.
Of course as others have said (and I do)
< file command1 | command2 | command3
Performs pretty much the same thing. That said, cat is pretty small and won't bring your computer down if you use it when you don't really need to.
Normally using cat
vs directly hitting a file doesn't change anything, but it does make a difference for certain commands that care if there are multiple files as arguments, such as grep
. Case in point:
cat file1 file2 | grep SOMETHING
will have different output than
grep SOMETHING file1 file2
Which will have the matching filenames in the output. There are times I don't want the filenames, and it's an advantage using cat
.
There is no advantage. Your cursor being at the end also doesn't matter much if you structure it like this instead: < inputfile grep -args foo