[mdlug] UNIX tips: Learn 10 good UNIX usage habits

Aaron Kulkis akulkis3 at hotpop.com
Mon Mar 10 03:40:36 EDT 2008


Michael Corral wrote:
> 2008-03-10, Monsieur Robert Citek a ecrit:
>> contrast the output of these commands:
>>
>> $ cat /etc/passwd /etc/group | grep root
>> $ grep root /etc/passwd /etc/group
> 
> grep -h root /etc/passwd /etc/group
> 
> That will give the same output as the cat+pipe version.
> So calling cat is unnecessary in this example, since the one grep
> command does the same as two commands and a pipe.
> 
> In my experience I've found that calling cat is unnecessary about 95%
> of the time.
> 

The only times cat is necessary...

1: you want the to use one of the flags of cat, such as:
    cat -n file1 | cmd
    to prepend each line with a line number.

2: Multiple files to be processed as a single input stream.

    cat file1 file2 [file3...] | cmd


For all other cases, if the command does not have the ability
to read from a file named as a command line argument, file
redirection ALWAYS works (it's set up by the shell)


$ cat file | cmd1 [flags and args here] | cmd2

is better done as

$ cmd [flags and args here] < file | cmd2

and you can EVEN do this

$ < file cmd1 [flags and args here] | cmd2


Anything else is Gratuitous Use of cat -- 5 yard penalty.





More information about the mdlug mailing list