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.
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.
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
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.
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
Posts: 63
Rep:
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...
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.