LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-12-2012, 06:50 PM   #1
debsawyer
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
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?
 
Old 11-12-2012, 06:53 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by debsawyer View Post
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.
 
2 members found this post helpful.
Old 11-12-2012, 07:10 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
Old 11-12-2012, 08:56 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
Code:
grep --help    # Basic help information
man grep       # grep's manpages.  Detailed information
info grep      # grep's texinfo documentation.  Even more detailed information
 
1 members found this post helpful.
Old 11-12-2012, 11:25 PM   #5
debsawyer
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
I think I have it

grep -r 111 /myfile.txt ?
 
Old 11-13-2012, 12:09 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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.

Last edited by Wim Sturkenboom; 11-13-2012 at 12:17 AM.
 
Old 11-13-2012, 12:20 AM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Quote:
Originally Posted by Wim Sturkenboom View Post
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.
 
Old 11-13-2012, 12:22 AM   #8
freelinuxtutorials
Member
 
Registered: Oct 2009
Posts: 70

Rep: Reputation: 21
grep 111 myfile.txt
cat myfile.txt | grep 111
 
Old 11-13-2012, 12:25 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Quote:
Originally Posted by freelinuxtutorials View Post
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.
 
2 members found this post helpful.
Old 11-13-2012, 02:14 AM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by evo2 View Post
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
 
Old 11-13-2012, 03:42 PM   #11
jefro
Moderator
 
Registered: Mar 2008
Posts: 22,008

Rep: Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629
So what is the command?
 
Old 11-13-2012, 04:21 PM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by jefro View Post
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 View Post
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.

Last edited by johnsfine; 11-13-2012 at 04:32 PM.
 
Old 11-13-2012, 04:31 PM   #13
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
Old 11-13-2012, 04:46 PM   #14
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by TobiSGD View Post
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).
 
Old 11-13-2012, 05:31 PM   #15
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
College Homework Help, Statistics Homework Help, Finance Homework Help, Accounting marklaren Linux - Software 1 12-25-2009 06:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration