grep 与 cat 和 grep 之间的区别 [英] difference between grep Vs cat and grep

查看:223
本文介绍了grep 与 cat 和 grep 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下 2 个命令之间的区别,我知道应该使用 2) 但我想知道 1) 和 2) 中发生的确切顺序假设文件名有 200 个字符

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 has 200 characters in it

1) cat 文件名 |grep 正则表达式

1) cat filename | grep regex

2) grep 正则表达式文件名

2) grep regex filename

推荐答案

在功能上(就输出而言),这两者是相同的.第一个实际上创建了一个单独的进程 cat,它只是将文件的内容发送到标准输出,它显示在 grep 的标准输入上,因为 shell 有用管子把两者连接起来.

Functionally (in terms of output), those two are the same. The first one actually creates a separate process cat which simply send the contents of the file to standard output, which shows up on the standard input of the grep, because the shell has connected the two with a pipe.

从这个意义上说,grep regex 也是等价的,但过程少了一个.

In that sense grep regex <filename is also equivalent but with one less process.

当额外信息(文件名)被grep使用时,您将开始看到不同之处在于变体,例如:

Where you'll start seeing the difference is in variants when the extra information (the file names) is used by grep, such as with:

grep -n regex filename1 filename2

它与的区别:

cat filename1 filename2 | grep -n regex

是前者知道单个文件,而后者将其视为一个文件(没有名称).

is that the former knows about the individual files whereas the latter sees it as one file (with no name).

虽然前者可能会给你:

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

后者会更像:

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

<小时>

如果知道文件名,另一个可执行文件的行为会有所不同,它是 wc,字计数器程序:

$ 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

这篇关于grep 与 cat 和 grep 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆