LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Homework, need help? (https://www.linuxquestions.org/questions/linux-newbie-8/homework-need-help-4175436849/)

debsawyer 11-12-2012 07:50 PM

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?

custangro 11-12-2012 07:53 PM

Quote:

Originally Posted by debsawyer (Post 4827942)
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?

Best way to get help is to post what you've tried so far.

TobiSGD 11-12-2012 08:10 PM

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.

towheedm 11-12-2012 09:56 PM

Code:

grep --help    # Basic help information
man grep      # grep's manpages.  Detailed information
info grep      # grep's texinfo documentation.  Even more detailed information


debsawyer 11-13-2012 12:25 AM

I think I have it
 
grep -r 111 /myfile.txt ?

Wim Sturkenboom 11-13-2012 01:09 AM

Quote:

Code:

grep -r 111 /myfile.txt

This will search through a file myfile.txt that is located in the system's root directory. I doubt very much it is there; if it is, it will work.

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
If you're already in the directory where the file is, you can leave the path to the file out.
Code:

grep 111 myfile.txt
One drawback of the -r option is that it might give you - what seem to be - false negatives when searching with wildcards (if one does not understand how 'it' works).
Code:

filemanager@wim-desktop:~/filemanagertest/_backup$ grep -r 111 *.txt
grep: *.txt: No such file or directory

filemanager@wim-desktop:~/filemanagertest/_backup$ grep -r 111 *
copy_local2ftp_excsub_excemp_withbackup_2012-11-12/myfile.txt:111

The result of the first command can be interpreted as 'there are no files in this directory or its subdirectories' that end with '.txt', but the second command shows that that is not true.

If you don't know exactly where the file is, you can use find and execute a grep on the result.

evo2 11-13-2012 01:20 AM

Hi,

Quote:

Originally Posted by Wim Sturkenboom (Post 4828065)
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).

The -r option can be useful if you *don't* want to use wildcards. Consdier the case where the "FILE" argument is actually a directory.

Evo2.

freelinuxtutorials 11-13-2012 01:22 AM

grep 111 myfile.txt
cat myfile.txt | grep 111

evo2 11-13-2012 01:25 AM

Hi,

Quote:

Originally Posted by freelinuxtutorials (Post 4828071)
grep 111 myfile.txt
cat myfile.txt | grep 111

It's today's winner of the "Useless use of cat" award! Plus a bonus of just giving the homework answer even though the OP was making progress thanks to the helpful posts of others.

Double fail.

Evo2.

Wim Sturkenboom 11-13-2012 03:14 AM

Quote:

Originally Posted by evo2 (Post 4828069)
The -r option can be useful if you *don't* want to use wildcards. Consdier the case where the "FILE" argument is actually a directory.

Thanks, learned something again ;)

jefro 11-13-2012 04:42 PM

So what is the command?

johnsfine 11-13-2012 05:21 PM

Quote:

Originally Posted by jefro (Post 4828686)
So what is the command?

Based on the above discussion, that seems to depend on the meaning of the phrase:

Quote:

Originally Posted by debsawyer (Post 4827942)
a file called myfile.txt

Some (I expect including the instructor who assigned the problem) seem to assume that is equivalent to
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.

TobiSGD 11-13-2012 05:31 PM

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
is the one that is meant here.

johnsfine 11-13-2012 05:46 PM

Quote:

Originally Posted by TobiSGD (Post 4828717)
I would think that this excercise is simply assuming that the file is in the user's home-directory

I hope you are wrong (stopping short of saying I think you are wrong).

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).

TobiSGD 11-13-2012 06:31 PM

I can't see why that is not intuitive. It is like
Code:

grep for the combination 111 in the file myfile.txt
Just like you would say it in English, just shortened.


All times are GMT -5. The time now is 07:51 PM.