How can I ‘cat’ a man page like I would ‘cat’ a file to get just a dump of the contents?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
To get an ASCII man page file, without the annoying backspace/underscore attempts at underlining, and weird sequences to do bolding:
man ksh | col -b > ksh.txt
Method 2
First of all, the man files are usually just gziped text files somewhere in your file system. Since your milage will vary finding them and you probably wanted the processed and formatted version that man gives you instead of the source, you can just dump them with the man tool. By looking at man man, I see that you can change the program used to view man pages with the -P flag like this:
man -P cat command_name
It’s also worth nothing that man automatically detects when you pipe it’s output instead of viewing it on the screen, so if you are going to process it with something else you can skip straight to that step like so:
man command_name | grep search_string
or to dump TO a file:
man command_name > formatted_man_page.txt
Method 3
Man pages are usually troff pre-processed files, and you can get to the plain text with,
groff -t -e -mandoc -Tascii manpage.1 | col -bx > manpage.txt
groff is a wrapper for troff.
You might need to use gzip to uncompress the man page files first, and you’ll still have plenty of formatting information in the output.
Method 4
I do this all the time. This command line makes me happy:
man man | col -bx > man.txt
col -b removes backspaces.
col -bx also replaces tabs with spaces which is my strong preference.
If I want the text to be formatted to a width of my preference while reading, then I change the command to this:
MANWIDTH=10000 man man | col -bx > man.txt
Method 5
Just use the man command – you can pipe the output into other things just as you can with cat for a file.
Method 6
If you just want to cat a manpage, you can simply pipe it to cat:
man ls | cat
If you want to dump its content to a file:
man ls > ls_manpage_dump.txt
Method 7
A possible helper might look as follows:
#!/usr/bin/env sh
{
# We can safely export the following variables unless we source this file
export TERM=dumb
export MANPAGER=cat
export MANWIDTH=100
# Here is how it works:
#
# 1. 'col -b' removes backspaces, 'col -x' replaces tabs with spaces
# 2. Drop lines from the top up to USAGE word
# 3. Drop two lines from the bottom
man "$1"
| col -bx
| grep -A 100 USAGE
| sed '$d' | sed '$d'
} > "$2"
Usage:
$ mandump ksh ksh.txt
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0