Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
grep --help # Basic help information
man grep # grep's manpages. Detailed information
info grep # grep's texinfo documentation. Even more detailed information
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.
Last edited by Wim Sturkenboom; 11-13-2012 at 12:17 AM.
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.
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.
Based on the above discussion, that seems to depend on the meaning of the phrase:
Quote:
Originally Posted by debsawyer
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.
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.