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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
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
Location: a warm beach, cool ocean breeze, nice waves, and a Margaritta
Distribution: RHEL 5.5 Tikanga
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.
echo hello|fgrep --with-filename hello
It prints out
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
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.
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:
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:
$ cat > my_test_file.txt
$ ls *.txt
$ fgrep Hello *.txt
$ fgrep Hello *.txt /dev/null