Homework, need help?
Here is the question:
Type in the command grep - - help to access the help manual. Using this information and the information from the text, how would you write a command to find the pattern 111 in a file called myfile.txt? |
Quote:
|
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
|
Code:
grep --help # Basic help information |
I think I have it
grep -r 111 /myfile.txt ?
|
Quote:
The use of the recursive option (-r) is only useful in combination with wildcards; it will give you all files that contain '111' (if used correctly; see below). Assuming the file is located in your home directory, this is the command that always works. Code:
grep 111 /home/yourusername/myfile.txt Code:
grep 111 myfile.txt Code:
filemanager@wim-desktop:~/filemanagertest/_backup$ grep -r 111 *.txt If you don't know exactly where the file is, you can use find and execute a grep on the result. |
Hi,
Quote:
Evo2. |
grep 111 myfile.txt
cat myfile.txt | grep 111 |
Hi,
Quote:
Double fail. Evo2. |
Quote:
|
So what is the command?
|
Quote:
Quote:
a file in the current directory named myfile.txt That makes the assignment pretty trivial and the answer has been given in one of the posts. Others (apparently including the OP) think that phrase might mean: a file that might be anywhere in the filesystem named myfile.txt So far as I understand, grep alone can't do that. (As others pointed out in this thread) a recursive grep uses the filename parameter to specify the root of the directory tree under which it looks at all files. It does not use the filename parameter to specify a filename that it looks for recursively. Edit: Oops! I was incorrect. Grep does have another parameter documented in the man page for specifying the actual filename when recursively searching directories. I hate man pages. They make this kind of thing very hard to dig out. |
Since the file is called myfile.txt, which indicates that it is one of the user's files, I would think that this excercise is simply assuming that the file is in the user's home-directory, which is usually the current directory when you open a new terminal to begin the exercise. So I assume that the command
Code:
grep 111 myfile.txt |
Quote:
If the assignment is for the harder alternative for that assumption, then first it teaches the student to look at the man page carefully as I did the second time today, not carelessly as I did when I first replied. Second it teaches a valuable, but neither intuitive nor well documented, feature of grep. It is unfortunate that important features like that might be neither intuitive nor well documented. But if such is the case, that increases the importance of teaching them. (I always use GUI tools for such tasks, because I tend to forget command line obscurities and can't easily regain that knowledge from man pages). |
I can't see why that is not intuitive. It is like
Code:
grep for the combination 111 in the file myfile.txt |
Thanks. Hopefully the OP got something out of this. I did.
|
All times are GMT -5. The time now is 03:46 PM. |