Wednesday, September 12, 2007

fmt - simple optimal text formatter

Here's a quick and simple command line drive text formatter using fmt.

Fmt binary file is part of fedora core utility package from GNU. Fmt reformats a a human readable file based on columnar value specified as argument. Fmt also supports splitting of long line, identation of first line, readjustment of file column width.

INSTALLATION
============

Core utils is installed by default fresh installation. However, if you had done a system cleanup, the package might have been uninstalled. Fmt can be installed to Fedora by installing core utils binary package using yum like so

# yum -y install coreutils


FMT USAGE
=========

# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a long line of words that is useless for now.
This file is an example of human readable text file.
I am going to paste this file into my Linux SysAd blog!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Uniform spacing is also supported by fmt linux command. Uniform spacing is one space between words and 2 spaces after sentences.

# fmt -u testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a long line of words that is useless for now. This file is an
example of human readable text file. I am going to paste this file into
my Linux SysAd blog!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To change and adjust a standard column width or columnar line width to 5 would be like so

# fmt -w 5 testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a
long line of
words that
is useless
for now.
This file is
an example of
human readable
text file.
I am going
to paste this
file into my
Linux SysAd
blog!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now, let's modify our text file to give way for more sample fmt usage
# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a long line of words that is useless for now.This file is an example of human readable text file.

I am going to paste this file into my Linux SysAd blog! This is a long line of words that is useless for now.This file is an example of human readable text file.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Using a crown-margin argument with fmt would be

# fmt -c testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a long line of words that is useless for now.This file is an
example of human readable text file.

I am going to paste this file into my Linux SysAd blog! This is a
long line of words that is useless for now.This file is an example
of human readable text file.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Using a tagged paragraph with fmt would be
# fmt -t testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a long line of words that is useless for now.This file is an
example of human readable text file.

I am going to paste this file into my Linux SysAd blog! This is a
long line of words that is useless for now.This file is an example of
human readable text file.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Altenatively, using standard input would be like

# cat testfile.txt | fmt -u -
# fmt -u - < testfile.txt

For more fmt info, man fmt

HTH

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy