Skip to content

Instantly share code, notes, and snippets.

@garybernhardt
Created October 29, 2014 19:45
Show Gist options
  • Select an option

  • Save garybernhardt/e4910b80e8ccda9f3538 to your computer and use it in GitHub Desktop.

Select an option

Save garybernhardt/e4910b80e8ccda9f3538 to your computer and use it in GitHub Desktop.
find $(manpath | tr ':' '\n') -iname '*.1' | xargs cat | (LC_CTYPE=C tr -C '[:alnum:]-_' '\n') | egrep '^--[\-_[:alnum:]]+$' | sort | uniq -c | sort -n
@igalic

igalic commented Oct 29, 2014

Copy link
Copy Markdown
egrep: Invalid range end
xargs: cat: terminated by signal 13

@garybernhardt

Copy link
Copy Markdown
Author

your grep has a different syntax; debug it and I'll update

@fj

fj commented Oct 29, 2014

Copy link
Copy Markdown

Using grep -P instead of egrep should work everywhere:

find $(manpath | tr ':' '\n') -iname '*.1' | xargs cat | (LC_CTYPE=C tr -C '[:alnum:]-_' '\n') | grep -P '^--[\-_[:alnum:]]+$' | sort | uniq -c | sort -n
      1 --crate-type
      1 --emit
      1 --html-after-content
      1 --html-before-content
      1 --html-in-header
      1 --input-format
      1 --library-path
      1 --no-defaults
      1 --output
      1 --output-format
      1 --plugin-path
      1 --plugins
      1 --release
      1 --target
      2 --help
      2 --passes
      4 --init
      5 --proxy-server

http://twitter.com/jxxf/

@garybernhardt

Copy link
Copy Markdown
Author

$ grep -P
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]

@garybernhardt

Copy link
Copy Markdown
Author

wait, I suck

@garybernhardt

Copy link
Copy Markdown
Author

Wait, no, I don't suck. My grep's usage says that it supports -P. But the man page doesn't list -P, and trying to use it results in usage. OK.

$ grep -P foo
usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]

@igalic

igalic commented Oct 29, 2014

Copy link
Copy Markdown

heh… unix…

igalic@levix ~ % find $(manpath | tr ':' '\n') -iname '*.1' | xargs cat | (LC_CTYPE=C tr -C '[:alnum:]-_' '\n') | grep -P '^--[\-_[:alnum:]]+$'
-----------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
-----------------------------------------------------------------
igalic@levix ~ % 

(i got the same when i just replaced egrep with ack [which is essentially perl])

@igalic

igalic commented Oct 29, 2014

Copy link
Copy Markdown

(this is some bitter sweet irony ;)

@fj

fj commented Oct 29, 2014

Copy link
Copy Markdown

cool, looks like Apple removed -P in 10.8 but never updated the spec for it: http://stackoverflow.com/questions/16658333/grep-p-no-longer-works-how-can-i-rewrite-my-searches

computers

@garybernhardt

Copy link
Copy Markdown
Author

can someone not on OS X try this version, with perl instead of grep?

find $(manpath | tr ':' '\n') -iname '*.1' | xargs cat | (LC_CTYPE=C tr -C '[:alnum:]-_' '\n') | perl -ne 'print if /^--[\w-]+$/' | grep -v '^-*$' | sort | uniq -c | sort -n

@colby

colby commented Oct 29, 2014

Copy link
Copy Markdown

Err, not on OSX.

DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"
vagrant@utility01:~$ find $(manpath | tr ':' '\n') -iname '*.1' | xargs cat | (LC_CTYPE=C tr -C '[:alnum:]-_' '\n') | perl -ne 'print if /^--[\w-]+$/' | grep -v '^-*$' | sort | uniq -c | sort -n
vagrant@utility01:~$ echo $?
0

@garybernhardt

Copy link
Copy Markdown
Author

try replacing LC_CTYPE with LC_ALL

@garybernhardt

Copy link
Copy Markdown
Author

well @colby I still want to figure out why tr is bailing like that

@colby

colby commented Oct 29, 2014

Copy link
Copy Markdown

Sorry, updated with something not OSX. I would assume you're on OSX.

@colby

colby commented Oct 29, 2014

Copy link
Copy Markdown

Running same VM as above.

vagrant@utility01:~$ find $(manpath | tr ':' '\n') -iname '*.1' | xargs cat | (LC_ALL=C tr -C '[:alnum:]-_' '\n') | perl -ne 'print if /^--[\w-]+$/' | grep -v '^-*$' | sort | uniq -c | sort -n
vagrant@utility01:~$ echo $?
0

@garybernhardt

Copy link
Copy Markdown
Author

I give up, fuck computers

@mgedmin

mgedmin commented Oct 29, 2014

Copy link
Copy Markdown

I'm on Ubuntu 14.10. Your command produces no output.

(That would be because man pages are compressed on Debian. Change the find to use -iname '*.1' -o -iname '*.1.gz' and xargs cat to xargs zcat.)

Also, the final sort should be sort -n, otherwise it puts 93 between 8 and 9. This is be locale-specific -- LC_COLLATE=C sort doesn't ignore leading whitespace.

@garybernhardt

Copy link
Copy Markdown
Author

the final sort is sort -n

@mgedmin

mgedmin commented Oct 29, 2014

Copy link
Copy Markdown

Now I'm wondering what are those 162 commands that support --1v. And I suspect 28 -----syntax and 17 -----examples aren't really options.

@mgedmin

mgedmin commented Oct 29, 2014

Copy link
Copy Markdown

(It's difficult to copy and paste a line of text from a horizontally-scrolling div. I accidentally the final -n. Sorry.)

@schmonz

schmonz commented Oct 29, 2014

Copy link
Copy Markdown
:; find $(manpath | tr ':' '\n') -iname '*.1' | xargs cat | (LC_CTYPE=C tr -C '[:alnum:]-_' '\n') | perl -ne 'print if /^--[\w-]+$/' | grep -v '^-*$' | sort | uniq -c | sort -n
ksh: manpath: not found
tr: unknown option -- C
usage: tr [-cs] string1 string2
       tr [-c] -d string1
       tr [-c] -s string1
       tr [-c] -ds string1 string2
find: unknown option -- i
find: unknown option -- n
find: unknown option -- a
find: unknown option -- m
find: unknown option -- e
find: *.1: No such file or directory

:; uname -mrs
NetBSD 6.1.5 amd64

(FWIW, if I were chasing portability in shell programming, I'd probably punt on making this a one-liner. Make a script, define well-named functions, make their composition evident, then futz with implementation details of each function.)

@garybernhardt

Copy link
Copy Markdown
Author

OS X's zcat append ".Z" to all paths not already ending with it, so that doesn't work. Using gzcat instead fails because it tries to decompress everything, so uncompressed man pages blow up. I bet that Ubuntu has saved its users as much as ten megabytes of disk space by compressing the man pages! :/

@garybernhardt

Copy link
Copy Markdown
Author

triple click selects full lines of text, which is useful for situations like this (although I don't know whether various Linux dinguses will do the right thing there; probably not)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment