LinuxQuestions.org
Visit Jeremy's Blog.
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 03-18-2014, 04:46 AM   #1
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Rep: Reputation: 1
a question on grep


Hello,

I have encountered this question in the book: "linux essentials". I apologize to put it here but I really don't understand what the question wants to ask and its answer is not helping me either:

Quote:
fgrep is called with more than one file name on the command line, it outputs the name of the file in question in front of every matching line. This is possibly a problem if you invoke grep with a hell wildcard pattern (such as *.txt), since the exact format of the grep output cannot be fore-seen, which may mess up programs further down the pipeline. How can you enforce output of the file name, even if the search pattern expands to a single file name only? (Hint:There is a very useful file in /dev.
I'd be grateful if you help me understand this question and if my post should be posed somewhere else pls let me know.
 
Old 03-18-2014, 05:42 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
1. is this ok?
fgrep is called with more than one file name on the command line, it outputs the name of the file in question in front of every matching line
in short: fgrep reports not only the matched line but the filename. Just try it to check what does it mean.

2. Do you know: fgrep does not print filename if only one single file was given on the command line.

3. is this ok?
This is possibly a problem if you invoke grep with a hell wildcard pattern (such as *.txt) ... because you will have no idea ... if the search pattern expands to a single file name only.

4. the original question: how can you enforce output of the file name to avoid that problem?

The solution can be find in the man page, see output line prefix control
 
Old 03-18-2014, 06:32 AM   #3
sryzdn
Member
 
Registered: Feb 2013
Posts: 248

Original Poster
Rep: Reputation: 1
Quote:
2. Do you know: fgrep does not print filename if only one single file was given on the command line.
The problem I have is: fgrep prints the file name even if only one file name was given in the command line.
Quote:
This is possibly a problem if you invoke grep with a hell wildcard pattern (such as *.txt) ... because you will have no idea ... if the search pattern expands to a single file name only.
well, when I test "fgrep lablablab *.txt", it returns all the file names even when we have only one file. So, why should there be a problem?
Quote:
4. the original question: how can you enforce output of the file name to avoid that problem?
Problem is: whether or not I enforce the output to print the file name it does it.

Quote:
The solution can be find in the man page, see output line prefix control
--with file-name: This is the default when there is more than one file to search. But I have seen that it prints the single file name too.

Quote:
Hint:There is a very useful file in /dev
I'd be happy if you let me know which file
 
Old 03-18-2014, 07:23 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,133

Rep: Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273Reputation: 2273
this is not the default behaviour, I think there is an environment variable set GREP_OPTIONS. Again, see man page about it.
 
Old 03-18-2014, 10:20 AM   #5
tabbygirl1990
Member
 
Registered: Jul 2013
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63

Rep: Reputation: 1
sryzdn

i understand your question. i also understand the two posts given so far are not really helpful in answering your question. i always hate it when someone tells me, "read the man pages" as if i hadn't already done that and that the man pages would answer all of my question. sure it's in the man pages, but to me, often the man pages are more cryptic than the question.

don't give up on this, there are people here who can and will help you...

tabby
 
1 members found this post helpful.
Old 03-18-2014, 11:06 AM   #6
s.verma
Member
 
Registered: Oct 2013
Distribution: Debian Sid, Gentoo, Arch, Debian
Posts: 169
Blog Entries: 4

Rep: Reputation: 24
Dear sryzdn,

Quote:
Hint:There is a very useful “file” in /dev
Although I am not giving you 100% guarantee that I understood and got your('s book) question correctly. But as I have got it, there is a file /dev/stdin, when you are taking something from standard input i.e. /dev/stdin then e.g.

Code:
echo hello|fgrep --with-filename hello
It prints out

(standard input):hello
instead of
hello

Thus it gives you additional information that you have searched in standard input rather than a file, and all this you get by using
Code:
fgrep --with-filename
or
Code:
fgrep -H
option.
Quote:
How can you enforce output of the file name, even if the search pattern expands to a single file name only?
Thus enforcing output of file name even for single file.
 
Old 03-18-2014, 12:16 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,968

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Indeed, the easiest way to force the file name to be displayed is to use the "-H" (--with-filename) option. Before that option existed, the trick for forcing the name to be displayed was
Code:
grep "pattern" /dev/null *.txt
That way, even if "*.txt" expands to just one name, grep will still see more than one file name argument, and of course /dev/null will never contain a match for any pattern.
 
2 members found this post helpful.
Old 03-18-2014, 12:17 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Some versions of grep (cfr. AIX) hasn't got the -H option doing the same as GNU grep. Therefore a trick to enforce the file name in the grep output (and ensure portability) is to make it believe you are looking for the pattern into an additional "file" that never matches. Following the hint suggested by the book, you can try:
Code:
fgrep pattern *.txt /dev/null
In this way, even if the wildcard expands to a single .txt file, grep will print out the filename in the output. Remember that the default behavior is: print the filename (followed by a colon) only if there are multiple files to search.

A practical example:
Code:
$ cat > my_test_file.txt
Hello world
<Ctrl-D>
$ ls *.txt
my_test_file.txt
$ fgrep Hello *.txt
Hello world
$ fgrep Hello *.txt /dev/null
my_test_file.txt:Hello world
Hope this helps.

Edit: beaten by rknichols for a bunch of seconds!

Last edited by colucix; 03-18-2014 at 12:18 PM.
 
2 members found this post helpful.
  


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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
question on grep anurupr Linux - Newbie 2 03-14-2010 09:36 AM
[SOLVED] grep question aihaike Programming 3 07-28-2009 01:43 PM
grep question torrent478 Linux - Newbie 2 10-08-2008 07:50 PM
Question about Grep irfanhab Linux - Newbie 6 08-23-2004 03:57 PM


All times are GMT -5. The time now is 09:18 PM.

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