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.
I can find whole words in manfiles with the grep command, but I can't find options.
I have a manfile: /home/a/manfiles/grep.man. I would like to use grep to find the option "-F".
My failed attempts:
Code:
1) $ grep -n '--text' /home/a/manfiles/grep.man
2) $ grep '-F, ' ~/manfiles/grep.man
grep: invalid option -- ','
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
3) $ grep -n '-F, ' ~/manfiles/grep.man
grep: invalid option -- ','
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
4) $ grep -w -F ~/manfiles/grep.man
^C
5) $ grep -n '-F' ~/manfiles/grep.man
^C
6) $ grep -n "-F" /home/a/manfiles/grep.man
^C
7) $ grep -f '-F' /home/a/manfiles/grep.man
grep: -F: No such file or directory
8) $ grep -f /home/a/manfiles/grep.man '-F'
^C
9) $ grep -fwr '-F' /home/a/manfiles/grep.man
grep: wr: No such file or directory
10) $ grep -wrf '-F' /home/a/manfiles/grep.man
grep: -F: No such file or directory
I have succeeded in finding words, e.g.
Code:
a@a-X200MA:/media/a/Sea_ext4/Calculus/Ch07$ grep -n '\<PATTERN\>' /home/a/manfiles/grep.man
9: grep [OPTIONS] PATTERN [FILE...]
10: grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
15: containing a match to the given PATTERN. By default, grep prints the
36: Interpret PATTERN as an extended regular expression (ERE, see
40: Interpret PATTERN as a list of fixed strings, separated by
45: Interpret PATTERN as a basic regular expression (BRE, see below).
49: Interpret PATTERN as a Perl regular expression (PCRE, see below).
54: -e PATTERN, --regexp=PATTERN
55: Use PATTERN as the pattern. This can be used to specify multiple
65: Ignore case distinctions in both the PATTERN and the input files.
Why can't grep find anything starting with a hyphen ("-F")?
Last edited by andrew.comly; 12-01-2015 at 10:00 PM.
$ grep -n '\-F' /home/a/manfiles/grep.man
19: available. egrep is the same as grep -E. fgrep is the same as grep -F.
39: -F, --fixed-strings
41: newlines, any of which is to be matched. (-F is specified by
It seems that symbol "-" is quite particular and necessitates the use of a backslash in order to negate(turn off) it's special nature. But of course this shows that my understanding of single quotes isn't quite right.
I wonder why in Bash Beginners Guide, p31 it is written "Single quotes ('...') are used to preserve the literal value of each character enclosed within the quotes", but in reality you still must use a backslash. Wouldn't that be a contradiction?
Last edited by andrew.comly; 12-01-2015 at 10:17 PM.
Reason: key understanding missing
Nope - it does retain its literal value, but gets passed to grep as an option - just like the other options (preceded with a hyphen).
Use "--" to end options, then pass in anything with a leading hyphen that is not an option.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.