Formatting the man command output for printing

  • by

1. Limiting the width of Linux man command output

Quite often, we need to print the output of the Linux man command. One way is to redirect the man command output to a file, open the file in LibreOffice Writer and export to PDF. The PDF can, later, be printed. However, by default, the man command output comprises of long lines which look great on computer's display but wrap over to next line when printed. One way is to manually format the text in LibreOffice Writer. But this is tedious. How to reduce the width of man command output? If the man command output could be limited to, say, 80 columns, then the text would not wrap over to next line in LibreOffice Writer and the man command output file could be exported to the PDF straightaway.

The solution lies in setting the environment variable MANWIDTH. For example, the following commands need to be given from the shell.

$ export MANWIDTH=80
$ man command > command.txt

Here, command is a Linux command like ls. The width of the output text is limited to MANWIDTH, which is 80 characters in this case. After this the file command.txt can be opened in LibreOffice Writer. You can add page number to pages by, first, click, Insert -> Footer -> Default. And, then, click inside the footer and select center or right justification of text in the footer. Finally, click, Insert -> Fields -> Page Number. Lastly, export the file to the PDF by clicking, File -> Export as PDF.

2. Filtering out control characters from man output

Some Unix systems like Solaris embed the man output with control characters like backspace (08) in order to make certain characters bold. For example, the sequence A (0x41) backspace (0x08) A (0x41) prints A in bold in the man command output on terminals. But when we open the redirected man command output file in an editor, we see a lot of control and repeating characters. We can get rid of these by using the col command. For example,

$ man command > command.txt
$ col -b < command.txt > command.new

The col command can only read and write standard input and output respectively. It does not read and write files directly.

3. Deleting blank lines

The command.new file might have multiple blank lines. These can be deleted by using the Perl one liner,

$ perl -00pe0 < command.new > command.out

4. Deleting lines containing a string

Some systems print a header line like, User Commands on each page. These lines are easily deleted using the Perl one liner,

perl -ni -e 'print unless /pattern/' filename

This deletes the lines containing pattern in the file, filename, in situ.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You may like these, also

  • POSIX message queues in LinuxPOSIX message queues in Linux 1.0 POSIX Message queues POSIX interprocess comunication (IPC) was introduced in the POSIX.1b standard (IEEE Std 1003.1b-1993) for real time…
  • POSIX Shared Memory in LinuxPOSIX Shared Memory in Linux 1.0 Shared Memory Shared memory is the fastest method of interprocess communication (IPC) under Linux and other Unix-like systems. The…
  • POSIX Semaphores in LinuxPOSIX Semaphores in Linux 1.0 Semaphores Semaphores are used for process and thread synchronization. Semaphores are clubbed with message queues and shared memory under…
  • fork and exec system calls in Linuxfork and exec system calls in Linux 1.0 fork and exec system calls Suppose we wish to write a "shell program" which would execute another program. Now,…
  • Connecting two computers with Ethernet LAN cableConnecting two computers with Ethernet LAN cable Quite often, we wish to connect two computers back to back using an Ethernet LAN cable. It may be because…
  • D-Bus TutorialD-Bus Tutorial 1.0 D-Bus D-Bus is a mechanism for interprocess communication under Linux and other Unix-like systems. D-Bus has a layered architecture.…
  • Socket programming using the select system callSocket programming using the select system call 1.0 Client-Server Paradigm The Client-Server paradigm divides the software architecture of a system in two parts, the server and its…
  • System V message queues in LinuxSystem V message queues in Linux 1.0 Message queues Message queues are one of the interprocess communication mechanisms available under Linux. Message queues, shared memory and…
  • POSIX Threads Synchronization in CPOSIX Threads Synchronization in C 1.0 POSIX Threads Synchronization POSIX Threads provide multiple flows of execution within a process. The threads have their own stacks…
  • System V Shared Memory in LinuxSystem V Shared Memory in Linux 1.0 Shared Memory Shared memory is one of the three interprocess communication (IPC) mechanisms available under Linux and other Unix-like…