Shell Script to extract various sections of a Man page

A Man page has various sections like

  1. NAME
  2. SYNOPSIS
  3. DESCRIPTION
  4. EXAMPLE
  5. AUTHORS

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,

  1. The Man page number (Commmands are usually under man page 1, system calls under man page 2, library calls under man page 3)
  2. Section: as described above, It can be NAME, DESCRIPTION
  3. Command

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s