ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Have you thought about it? You are telling grep to find every line but one and telling it to return those lines and the lines before them as well.
When you think about it, it should return every line but the last one and the ones containing QQQ+the line before twice. Problem is, it doesn't, and I couldn't find any explanation for that either.
2.1.5 Context Line Control
--------------------------
Regardless of how these options are set, `grep' will never print any
given line more than once. If the `-o' (`--only-matching') option is
specified, these options have no effect and a warning is given upon
their use.
2.1.5 Context Line Control
--------------------------
Regardless of how these options are set, `grep' will never print any
given line more than once. If the `-o' (`--only-matching') option is
specified, these options have no effect and a warning is given upon
their use.
Thanks. So the problem is not a software problem, but a documentation problem, this information is simply not present on the manpage (and to be quite frank, I never use info, I find it uncomfortable and hard to use).
I don't like the info pages myself and I only use them as a last resort when I can't find what I'm looking for in the man pages, just for case it happens to contain that one extra sentence that explains the problem.
I don't like the info pages myself and I only use them as a last resort when I can't find what I'm looking for in the man pages, just for case it happens to contain that one extra sentence that explains the problem.
I will keep that in mind. It never came to my mind that the manpages might not contain the same information as the info pages do.
They usually do, but not always. Often, the man page ends with a reference to info. For example, all the way down my grep manpage says:
Code:
TeXinfo Documentation
The full documentation for grep is maintained as a TeXinfo manual,
which you can read at http://www.gnu.org/software/grep/manual/. If the
info and grep programs are properly installed at your site, the command
info grep
should give you access to the complete manual.
NOTES
This man page is maintained only fitfully; the full documentation is
often more up-to-date.
To get back to the original question, I would suggest an alternative to the non working grep:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian 5.0.8 (lenny, workstation)
bash GNU bash 3.2.39
cgrep ATT cgrep 8.15
-----
Input data file data1:
Audi
Bentley
Chevrolet
Dodge
QQQ
Ford
Honda
Isuzu
Jaguar
QQQ
Kaiser
Lexus
-----
Results, cgrep:
Dodge
QQQ
Jaguar
QQQ
-----
Results, inverse cgrep:
Audi
Bentley
Chevrolet
Ford
Honda
Isuzu
Kaiser
Lexus
-----
Results, inverse cgrep, wrong -v (lowercase), expect failure:
Audi
Bentley
Chevrolet
Dodge
QQQ
Ford
Honda
Isuzu
Jaguar
QQQ
Kaiser
Lexus
One needs to compile it. I have done that in both 32-bit and 64-bit without problems.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.