A Man page has various sections like
The above are some of the few sections to name for. You open a man page using the command man. It can also be redirected to a file.
Let’s see the script
$ cat man_section_extract.sh if [ $# -ne 3 ] then echo "Usage: man_section_extract man_number section command" exit 1 fi man_number=$1 section=$2 command=$3 man $man_number $command> /tmp/man.txt if [ $? -eq 0 ] then cat /tmp/man.txt | sed -n -e "/^$section/,/^[A-Z]/ p"| grep -v -e "^[A-Z]" fi
As you can see that it takes three arguments,
- The Man page number (Commmands are usually under man page 1, system calls under man page 2, library calls under man page 3)
- Section: as described above, It can be NAME, DESCRIPTION
Let’s first take a look at a sample man page
NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort. Mandatory arguments to long options are mandatory for short options too.
Now let’s use our script to extract the NAME section
$ ./man_section_extract.sh 1 NAME ls ls - list directory contents
Let’s extract SYNOPSIS
$ ./man_section_extract.sh 1 SYNOPSIS ls ls [OPTION]... [FILE]...
Like this, you can extract various sections of a man page. All you need is to specify the command, section name and man page number