Unix has at all times excelled at processing textual content, and Linux isn’t any totally different. And the instruments to work with and rework textual content information nonetheless exist on all Linux programs.
Like different pc programs, early Unix printed on paper, utilizing a typewriter-style printing system. These printers supplied restricted formatting choices, however with intelligent software of Unix instruments, you may put together professional-looking paperwork.
One such device was the pr
device, to arrange textual content paperwork for printing. Let’s discover learn how to use commonplace Unix instruments, such because the pr
processor and the fmt
textual content formatter, to arrange textual content information for printing on a typewriter-style printer.
[ Read also: How I use the Linux fmt command to format text ]
Printing a plain textual content file
Let’s say we needed to print the MIT license, saved in a file known as mit.txt. This file is already formatted for optimum display show; traces are nearly 80 columns vast, which inserts effectively on a normal terminal.
$ cat mit.txt
Copyright (c) <yr> <copyright holders>
Permission is hereby granted, freed from cost, to any particular person acquiring a replica
of this software program and related documentation information (the "Software"), to deal
within the Software with out restriction, together with with out limitation the rights
to make use of, copy, modify, merge, publish, distribute, sublicense, and/or promote
copies of the Software, and to allow individuals to whom the Software is
furnished to take action, topic to the next situations:
The above copyright discover and this permission discover shall be included in all
copies or substantial parts of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Printer paper can be 80 columns vast, at the least on the traditional printers. So we are able to additionally print the file to our printer utilizing a command like lpr
. But that is not a really attention-grabbing method to print the file, and it will not be very good to learn. For instance, the doc will begin on the primary line of the printed web page, and instantly on the left fringe of the paper.
We could make the doc simpler to learn by utilizing the pr
command so as to add a high margin. By default, pr
contains the date and time, the identify of the file, and the web page quantity within the high header. For instance, the highest of our file would possibly seem like this:
$ pr mit.txt | head
2022-06-24 18:27 mit.txt Page 1
Copyright (c) <yr> <copyright holders>
Permission is hereby granted, freed from cost, to any particular person acquiring a replica
of this software program and related documentation information (the "Software"), to deal
within the Software with out restriction, together with with out limitation the rights
In this instance, I’ve used the head
command to have a look at simply the primary ten traces of the pr
output. The pr
command additionally provides further clean traces on the backside of the web page, to offer a backside margin. The old-style typewriter printers used 66 traces per web page, so the pr
output assumes that too. But this file prints on one web page, so I need not present the underside of the file; it is just a few clean traces.
Adding a left and proper margin
Adding the highest margin makes the doc simpler to learn, however we are able to do higher by including area on the left and proper of the printed web page. This successfully provides a left and proper margin to our doc.
The first step is to make use of the fmt
command to reformat the textual content file to a special width. Using fmt -w 70
reformats the textual content file to make use of traces which can be 70 columns vast. We can add some clean area on the left of the doc to create a left margin. Using pr -o 5
provides 5 areas to the beginning of every line of the output. With the narrower textual content, we’ll even have about 5 areas in the suitable margin.
$ fmt -w 70 mit.txt | pr -o 5 | head
2022-06-24 18:35 Page 1
Copyright (c) <yr> <copyright holders>
Permission is hereby granted, freed from cost, to any particular person acquiring
a replica of this software program and related documentation information (the
"Software"), to deal within the Software with out restriction, together with
This is how Unix customers printed plain textual content information. You can use the identical set of instructions to print textual content information on a contemporary laser printer, however your printer might anticipate a web page feed command as a substitute of utilizing clean traces. To do this, add the -f
choice to the pr
command, like this:
$ fmt -w 70 mit.txt | pr -f -o 5 | lpr
I’ll omit the -f
in the remainder of this text, however bear in mind so as to add -f
to the pr
command if you wish to print paperwork to a contemporary laser printer.
Changing the header
You might discover that after we redirect the output of fmt
to the pr
command, the pr
output now not exhibits the identify of the file. That’s as a result of after we chain a number of instructions collectively like this, the pr
command does not know the filename, so it is left clean. We can add the filename to the header by utilizing the -h
possibility:
$ fmt -w 70 mit.txt | pr -h 'mit.txt' -o 5 | head
2022-06-24 18:45 mit.txt Page 1
Copyright (c) <yr> <copyright holders>
Permission is hereby granted, freed from cost, to any particular person acquiring
a replica of this software program and related documentation information (the
"Software"), to deal within the Software with out restriction, together with
You could make different adjustments to the header, such because the -D possibility to vary the date and time format, or exchange it with new textual content.
$ fmt -w 70 mit.txt | pr -D '6/24/2022' -h 'mit.txt' -o 5 | head -30
6/24/2022 mit.txt Page 1
Copyright (c) <yr> <copyright holders>
Permission is hereby granted, freed from cost, to any particular person acquiring
a replica of this software program and related documentation information (the
"Software"), to deal within the Software with out restriction, together with
with out limitation the rights to make use of, copy, modify, merge, publish,
distribute, sublicense, and/or promote copies of the Software, and to
allow individuals to whom the Software is furnished to take action, topic
to the next situations:
The above copyright discover and this permission discover shall be
included in all copies or substantial parts of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Printing two columns
What in the event you needed to make a textual content doc look actually fancy on the printed web page? Certain paperwork corresponding to technical articles would possibly have to be printed in a two-column structure. The pr
command can print textual content in a number of columns. For instance, -2
prints in two columns and -3 will print in three columns.
However, watch out when printing textual content in a number of columns. If the traces are too lengthy, pr
might merely overlap one column with one other, successfully dropping textual content within the output. But we are able to leverage the fmt
command to reformat the textual content to a narrower width, appropriate for printing in two column format.
Let’s do the mathematics: If the printed web page is 80 columns vast, and we have left 5 areas on the left and proper as web page margins, that leaves 70 columns for our textual content. Using fmt -w 35
would minimize the textual content neatly in half for 2 columns, however we might not depart a lot area between the columns. Instead, let’s use fmt -w 33
to reformat the textual content width to 33 earlier than sending the output to the pr
command:
$ fmt -w 33 mit.txt | pr -2 -D '6/24/2022' -h 'mit.txt' -o 5 | head -30
6/24/2022 mit.txt Page 1
Copyright (c) <yr> <copyright be included in all copies or
holders> substantial parts of the
Software.
Permission is hereby granted,
freed from cost, to any particular person THE SOFTWARE IS PROVIDED
acquiring a replica of this "AS IS", WITHOUT WARRANTY OF
software program and related ANY KIND, EXPRESS OR IMPLIED,
documentation information (the INCLUDING BUT NOT LIMITED TO THE
"Software"), to deal within the WARRANTIES OF MERCHANTABILITY,
Software with out restriction, FITNESS FOR A PARTICULAR PURPOSE
together with with out limitation the AND NONINFRINGEMENT. IN NO
rights to make use of, copy, modify, EVENT SHALL THE AUTHORS OR
merge, publish, distribute, COPYRIGHT HOLDERS BE LIABLE
sublicense, and/or promote copies FOR ANY CLAIM, DAMAGES OR OTHER
of the Software, and to allow LIABILITY, WHETHER IN AN ACTION
individuals to whom the Software is OF CONTRACT, TORT OR OTHERWISE,
furnished to take action, topic to ARISING FROM, OUT OF OR IN
the next situations: CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN
The above copyright discover and THE SOFTWARE.
this permission discover shall
$
Unix is a good platform for processing textual content. While we use different instruments at the moment, together with HTML in internet browsers and PDF for printable content material, it is good to know learn how to use the prevailing Unix instruments to create skilled plain textual content paperwork.