Linux – Advantages of cat’ing file and piping to grep

command linegreplinuxpipeunix

Are there any additional advantages of cat'ing a file and piping it to grep, besides convenience? The convenience being that, when I retrieve commands such as those below from my history, the cursor is at the end of the line, so it is easy to modify the command with different text to grep against the same file.

So what other advantages might there be to the following convention:

cat /var/tmp/trace.2043925204.xt | grep -in profile
cat /var/tmp/trace.2043925204.xt | grep -n Profile-Main

instead of:

grep -in profile /var/tmp/trace.2043925204.xt 
grep -n Profile-Main /var/tmp/trace.2043925204.xt 

Best Answer

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.

Related Question