0

I created a file man df &> man_df

Then I want to fetch information about flag -x from the file Expected result is "-x, --exclude-type=TYPE"

But command grep "-x" man_df is not working

Command

grep "exclude-type" man_df 
       -x, --exclude-type=TYPE

is working but I want to use "-x" (short flag name"

1
  • 2
    This question is similar to: How to grep for a pattern that looks like an option?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented 21 hours ago

4 Answers 4

1

This is simply because quoting a string doesn't exclude it from being treated as an option from grep's perspective and your -x looks like an option to grep. For this use case, you should precede it with -- to tell grep to stop expecting options after this point. Your command becomes: grep -- -x man_df.

0

Found solution

 grep "^[[:space:]]*-x" man_df
1
  • While this works, but it doesn't answer the original source of the problem, check my answer. Commented yesterday
0

You can do:

$ man df | grep -A1 '^ *-x'
       -x, --exclude-type=TYPE
              limit listing to file systems not of type TYPE

But on a GNU system, I'd rather do:

info df

Which brings up the GNU coreutils info manual at the df invocation page (node).

Then press I (uppercase i) to search the index, enter -x there which brings this menu:

* Menu:

* -x:                                    od invocation.       (line  203)
* -x <1>:                                split invocation.    (line  135)
* -x <2>:                                General output formatting.
                                                              (line  126)
* -x <3>:                                cp invocation.       (line  420)
* -x <4>:                                shred invocation.    (line  164)
* -x <5>:                                df invocation.       (line  224)
* -x <6>:                                du invocation.       (line  261)
* -x <7>:                                Access permission tests.
                                                              (line   24)
* -X:                                    Sorting the output.  (line   96)
* -X FILE:                               du invocation.       (line  250)

(as the coreutils manual documents all GNU coreutils commands, not just df, many of which have a -x option).

Then navigate to that -x <5> entry for instance with Tab and press Enter (or press 6 as that -x <5>, confusingly is the sixth menu entry there) which will take you to:

‘-x FSTYPE’
‘--exclude-type=FSTYPE’
Limit the listing to file systems not of type FSTYPE. Multiple file system types can be eliminated by giving multiple ‘-x’ options. By default, no file system types are omitted.

Alternatively, you can press i instead of I which will jump to the first matching index entry and then press , repeatedly to cycle through all the matching entries.

When info's output is not going to a terminal, like when it's redirected to a file or pipe, then it just dumps the contents of the node (see also the --subnodes option²).

So you can also pipe to grep -- -x (where the regexp is passed as the first non-option argument) or grep -e -x / grep -e-x (where the regexp is passed as an argument to the -e option)¹ with some context with -C (like -A a GNU extension):

$ info df | grep -C2 -e -x
‘--print-type’
     Print each file system's type.  The types printed here are the same
     ones you can include or exclude with ‘-t’ and ‘-x’.  The particular
     types printed are whatever is supported by the system.  Here are
     some of the common names (this list is certainly not exhaustive):
--
          File systems used by MS-Windows / MS-DOS.

‘-x FSTYPE’
‘--exclude-type=FSTYPE’
     Limit the listing to file systems not of type FSTYPE.  Multiple
     file system types can be eliminated by giving multiple ‘-x’
     options.  By default, no file system types are omitted.

--
   Since the list of file systems (MTAB) is needed to determine the file
system type, failure includes the cases when that list cannot be read
and one or more of the options ‘-a’, ‘-l’, ‘-t’ or ‘-x’ is used together
with a file name argument.

¹ or use something like [-]x or \(-x\) as the regexp, which also match -x but don't start with -.

² While you can see man giving you a page of the system's one and only manual organised in sections 1 to 8, each texinfo manual can be seen as a separate book for the corresponding software (here the GNU coreutils collection of basic utilities) each with a hierarchical structure and a table of contents and one or more indexes like in real life books (and with the info browser, with the additional facility of accessing and searching that table of contents or index including with completion). And info also gives you a searchable directory of all the known such books on the system (in $INFOPATH) (similar to what apropos does for man).

0

Stéphane gave you the way for filtered text output.

if you'd like to do this in a interactive way, I recommend:

LESS=+'/^ +-x' man df

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.