difference between grep Vs cat and grep

后端 未结 5 1006
再見小時候
再見小時候 2020-12-29 05:53

i would like to know difference between below 2 commands, I understand that 2) should be use but i want to know the exact sequence that happens in 1) and 2) suppose filename

5条回答
  •  忘掉有多难
    2020-12-29 06:41

    First one:

    cat filename | grep regex
    

    Normally cat opens file and prints its contents line by line to stdout. But here it outputs its content to pipe'|'. After that grep reads from pipe(it takes pipe as stdin) then if matches regex prints line to stdout. But here there is a detail grep is opened in new shell process so pipe forwards its input as output to new shell process.

    Second one:

    grep regex filename
    

    Here grep directly reads from file(above it was reading from pipe) and matches regex if matched prints line to stdout.

提交回复
热议问题