LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 07: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, 07:53 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,970
Blog Entries: 1

Rep: Reputation: 208Reputation: 208Reputation: 208
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, 08:10 PM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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, 09:56 PM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
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-13-2012, 12:25 AM   #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, 01:09 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

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 01:17 AM.
 
Old 11-13-2012, 01:20 AM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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, 01: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, 01:25 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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, 03:14 AM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

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, 04:42 PM   #11
jefro
Moderator
 
Registered: Mar 2008
Posts: 15,374

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

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
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 05:32 PM.
 
Old 11-13-2012, 05:31 PM   #13
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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, 05:46 PM   #14
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
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, 06:31 PM   #15
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:14 AM


All times are GMT -5. The time now is 04:49 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration