简体   繁体   English

如果没有指定文件,为什么grep不起作用?

[英]Why doesn't grep work if a file is not specified?

I have some problem with the Linux grep command, it don't work !!! 我有一些Linux grep命令的问题,它不起作用!

I am trying the following test on my Ubuntu system: 我在我的Ubuntu系统上尝试以下测试:

  1. I have create the following folder: /home/andrea/Scrivania/prova 我创建了以下文件夹: /home/andrea/Scrivania/prova

  2. Inside this folder I have created a txt file named prova.txt and inside this file I have write the string test and I have save it 在这个文件夹里面我创建了一个名为prova.txt的txt文件,在这个文件中我写了字符串test并保存了

  3. In the shell I have first access the folder /home/andrea/Scrivania/prova and so I have launched the grep command in the following way: 在shell中我首先访问文件夹/home/andrea/Scrivania/prova ,因此我以下列方式启动了grep命令:

     ~/Scrivania/prova$ grep test 

The problem is that the cursor continues to blink endlessly and cannot find NOTHING! 问题是光标继续无休止地闪烁,找不到任何东西! Why? 为什么? What is the problem? 问题是什么?

You've not provided files for the grep command to scan 您没有为要扫描的grep命令提供文件

grep "test" *

or for recursive 或者递归

grep -r "test" *

Because grep searches standard input if no files are given. 因为如果没有给出文件,grep会搜索标准输入。 Try this. 尝试这个。

grep test *

You are not running the command you were looking for. 您没有运行您正在寻找的命令。

grep test * will look for test in all files in your current directory. grep test *将在当前目录的所有文件中查找test

grep test prova.txt will look for test specifically in prova.txt grep test prova.txt将在prova.txt专门寻找test

( grep test will grep the test string in stdin , and will not return until EOF .) grep test将在stdin grep test字符串,并且在EOF之前不会返回。)

You need to pipe in something to grep - you cant just call grep test without any other arguments as it is actually doing nothing. 你需要管道输入grep - 你不能在没有任何其他参数的情况下调用grep test,因为它实际上什么都不做。 try grep test * 尝试grep test *

Another use for grep is to pipe in a command grep的另一个用途是在命令中管道

eg This is my home directory: 这是我的主目录:

drwx------+  3 oliver  staff    102 12 Nov 21:57 Desktop
drwx------+ 10 oliver  staff    340 17 Nov 18:34 Documents
drwx------+ 17 oliver  staff    578 20 Nov 18:57 Downloads
drwx------@ 12 oliver  staff    408 13 Nov 20:53 Dropbox
drwx------@ 52 oliver  staff   1768 11 Nov 12:05 Library
drwx------+  3 oliver  staff    102 12 Nov 21:57 Movies
drwx------+  5 oliver  staff    170 17 Nov 10:40 Music
drwx------+  3 oliver  staff    102 20 Nov 19:17 Pictures
drwxr-xr-x+  4 oliver  staff    136 12 Nov 21:57 Public

If i run 如果我跑

l | grep Do

I get the result 我得到了结果

drwx------+ 10 oliver  staff    340 17 Nov 18:34 Documents
drwx------+ 17 oliver  staff    578 20 Nov 18:57 Downloads

remember to pipe the grep command 记得管道 grep命令

From grep man page: 从grep手册页:

Grep searches the named input FILEs (or standard input if no files are named, or the file name - is given) for lines containing a match to the given PATTERN. 对于包含与给定PATTERN匹配的行,Grep搜索命名输入FILE(如果没有命名文件,则为标准输入,或者给出文件名)。

If you don't provide file name(s) for it to use, it will try to read from stdin. 如果您没有提供要使用的文件名,它将尝试从stdin读取。

Try grep test * 尝试grep test *

As per GNU Grep 3.0 根据GNU Grep 3.0

A file named - stands for standard input. 名为-的文件代表标准输入。 If no input is specified, grep searches the working directory . 如果未指定输入, grep搜索工作目录. if given a command-line option specifying recursion; 如果给出一个指定递归的命令行选项; otherwise, grep searches standard input. 否则,grep搜索标准输入。

So for OP's command, without any additional specification, grep tries to search in standard input, which is not actually provided there. 因此对于OP的命令,没有任何额外的规范, grep尝试搜索标准输入,这实际上并没有在那里提供。

A simple approach is grep -r [pattern] , as per the above, to specify recursion with -r and search in current directory and sub-directories. 一个简单的方法是grep -r [pattern] ,按照上面的说法,-r指定递归并在当前目录和子目录中搜索。

Also note that wildcard * only includes files, not directories . 另请注意,通配符*仅包含文件, 而不包含目录 If used, a prompt might be shown for hint: 如果使用,可能会显示提示提示:

grep: [directory_name]: Is a directory grep:[directory_name]:是一个目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM