Overview
it’s error for use cat in Linux. Why say it? Let’s Go~
Generally
我们在Linux中如果需要从一个文件中过滤出我们specify content
,我们会使用:
cat file | grep -i "specify content"
cat
的功能就是将一个文件的内容展示出来,如果你在你的Linux中执行cat
这个指令,你会在终端中看到这个文件全部的stuff。在这里我们通过一个 pipe 将cat
所 output 到 terminal 的内容交给 grep 程序进行处理,
许多人都是这样做的,但是你需要知道的是,这样大可不必,你完全可以用一个更简单的方式来操作。当然,cat
还有许多其他的功能,但我们在这里仅仅讨论如果你只想展示文件的全部内容并且从中获取信息的话,我建议你这样做。
Started
here haven a file of name is file,that content following:
Here's my file.
It has some sentences in it.
There's one per line.
It's not very long.
We'll grep stuff out of it.
It's just an example.
Now, we do something. The $
for terminal CLI:
$ cat file | grep it
It has come sentences in it.
We'll grep stuff out of if.
You can use grep
:
$ grep "it" file
It has come sentences in it.
We'll grep stuff out of if.
你能得到相同的结果
Maybe you can say, I’m going to sed
to process output. But sed
same as grep
,you can direct input file
or files
.Look at:
$ cat file | sed 's#it#IT#g'
Here's my file.
It has some sentences in it.
There's one per line.
It's not very long.
We'll grep stuff out of it.
It's just an example.
$ sed 's#it#IT#g'
Here's my file.
It has some sentences in it.
There's one per line.
It's not very long.
We'll grep stuff out of it.
It's just an example.
现在教给你一个新的方法:
$ < file grep -i it
It has some sentences in it.
It's not very long.
We'll grep stuff out of it.
It's just an example.
# and more file
$ < file < Otherfile grep -i it.
直接从管道流中获取结果交给 grep
$ > file && < file
这样的做法请慎重,你必须知道你在做什么的时候才执行这段指令。因为它会从管道流中获取content传给 file 文件流。但是每个line都会刷新 channel 并且每读取一次就相当于取出一次,所以在新的一行中你如果这样执行会将一个 null 输出给 file。这将把你的 file 文件清空