The difference between grep Vs cat and grep

I would like to know the difference between the below 2 commands, I understand that 2) should be used, but I want to know the exact sequence that occurs in 1) and 2) suppose the file name contains 200 characters

1) cat filename | grep regex

2) grep regex file name

+8
unix
source share
5 answers

Functionally (in terms of output), these two values ​​are the same. The first of them actually creates a separate cat process, which simply sends the contents of the file to standard output, which is displayed on standard grep input, because the shell has connected two to the channel.

In this sense, grep regex <filename also equivalent, but with one smaller process.

If you start to see a difference in options when additional information (file names) is used by grep , for example:

 grep -n regex filename1 filename2 

The difference between this and:

 cat filename1 filename2 | grep -n regex 

lies in the fact that the former knows about individual files, while the latter sees it as a single file (without a name).

So far, the first one can give you:

 filename1:7:line with regex in 10-line file filename2:2:another regex line 

the latter will be more like:

 7:line with regex in 10-line file 12:another regex line 

Another executable that acts differently if it knows the file names is wc , a word counter program:

 $ cat qq.in 1 2 3 $ wc -l qq.in # knows file so prints it 3 qq.in $ cat qq.in | wc -l # does not know file 3 $ wc -l <qq.in # also does not know file 3 
+14
source share

First:

 cat filename | grep regex 

Typically, cat opens a file and prints its contents line by line in stdout. But here he displays his content on the '|' pipe. After that grep reads from the pipe (it takes pipe as stdin), then if match regex prints the string to stdout. But there is a grep detail that opens in a new shell process, so pipe sends its input as output to the new shell process.

Second:

 grep regex filename 

Here grep reads directly from the file (read from the channel above it) and matches the regular expression if it matches the prints line in stdout.

+4
source share

If you want to check the actual run-time difference, first create a file with 100,000 lines:

 user@server ~ $ for i in $(seq 1 100000); do echo line${1} >> test_f; done user@server ~ $ wc -l test_f 100000 test_f 

Now measure:

 user@server ~ $ time grep line test_f #... real 0m1.320s user 0m0.101s sys 0m0.122s user@server ~ $ time cat test_f | grep line #... real 0m1.288s user 0m0.132s sys 0m0.108s 

As we see, the difference is not too big ...

+1
source share

They are functionally equivalent, but the shell will fork two processes for cat filename | grep regex cat filename | grep regex and bind them to the channel.

0
source share

Actually, although the outputs are the same,

 -$cat filename | grep regex 

This command looks for the contents of the file "file_name", and then displays the regular expression in it; while

 -$grep regex filename 

This command directly searches for contents named regex in the file "filename"

0
source share

All Articles