LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2015, 09:57 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question grep: can't find options in manfile


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.
 
Old 12-01-2015, 10:13 PM   #2
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Unhappy [solved]

I finally got it!
Code:
$ 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
 
Old 12-01-2015, 10:22 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.

This is general for bash, not specific to grep.
 
1 members found this post helpful.
Old 12-02-2015, 10:29 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by syg00 View Post
This is general for bash, not specific to grep.
It's not implemented by bash, grep and many other programs do follow this convention.
 
1 members found this post helpful.
  


Reply

Tags
bash, grep, strings, symbols, words



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
How to find specific options using grep Nitron Linux - Newbie 5 10-04-2015 07:28 PM
Find & grep - how to return pathes, not grep phrases ? postcd Linux - General 2 11-25-2014 12:43 PM
[SOLVED] how to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 12:25 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
grep -v and -C options baidym Programming 4 01-03-2009 02:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 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
Open Source Consulting | Domain Registration