4 ms·
I use cat because often I wind up doing a number of different commands on the same file. It's a lot easier to edit the end of the line, especially if you're ju
by Goladus 17y ago
I use cat because often I wind up doing a number of different commands on the same file. It's a lot easier to edit the end of the line, especially if you're just adding another filter, than to go back and modify the beginning.
eg
cat file | less
cat file | grep thing
cat file | grep otherthing
cat file | grep otherthing | cut stuff
instead of
less file
grep thing file
grep otherthing file
grep otherthing file | cut stuff
- swolchok 17y agoSounds like you might want to learn about !$ (last token of previous command).
- Goladus 17y agoYeah that's interesting, but the second version is still more complicated to assemble and takes more keystrokes, assuming up arrow gives the entire previous command.
- swolchok 17y agoLet's count for your example. In all cases, I'll exclude the actual name of the file. We type in the first command: cat file | less less file The second version is 5 fewer keystrokes. On to the next command: cat file | grep thing grep thing !$ The second example is the same or fewer keystrokes. In both cases, you have to type "grep thing". In the first, you have to press the up arrow and backspace over "less" (at least three keystrokes), and in the second, you have to type an extra " !$". I'll skip moving to "cat file | grep otherthing" or "grep otherthing !$", and consider the change to get to cat file | grep otherthing | cut stuff grep otherthing file | cut stuff In both cases, you have to type " | cut stuff". If you key in the second example as "!! | cut stuff", that's an extra two keystrokes. If you key in the first as up arrow + "| cut stuff", that's only 1 extra keystroke. In total, my version saves keypresses in the specific example and doesn't seem much worse in general.
- prodigal_erik 17y agoBe aware that "cat file | less" is much more expensive than "less file" because it forces less to buffer everything it reads (it can't seek on a pipe). "cat huge-logfile | tail" is especially bad because it uselessly reads the whole file (and evicts a bunch of more important data from your buffer cache) where "tail huge-logfile" would just seek backwards from the end until it has enough text.