Home  >  Article  >  System Tutorial  >  Format text using Linux fmt

Format text using Linux fmt

WBOY
WBOYforward
2024-02-14 11:30:351091browse

fmt command is a simple text formatter. I'll show you how to use it here to format text and email responses.

使用Linux fmt格式化文本

When I write documentation for a project, I often write readme files and installation instructions in plain text. I don't need to use a markup language like HTML or Markdown to describe what the project does or how to compile it. But maintaining such documentation can be a pain.

If I need to update the middle of a sentence in my Readme file, I need to reformat the text to avoid having a long or short line in the middle of my other text, while the other lines are formatted There are 75 neat columns. Some editors include features that automatically reformat text to fill paragraphs, but not all editors do this. This is where the Linux fmt command comes in.

Use Linux fmt command to format text

The

fmt command is a simple text formatter; it collects words and fills paragraphs, but does not apply any other text styles, such as italics or bold. It's all plain text. Using the fmt command, you can quickly adjust text to make it easier to read. Let's start with this familiar example text:

$ cat trek.txt 
Space: the final
frontier. These are the voyages
of the starship Enterprise. Its
continuing mission: to explore
strange new worlds. To
seek out new life and new
civilizations. To boldly go
where no one has gone before!

In this example file, each line has a different length, and they wrap in a weird way. If you make a lot of changes to a plain text file, you may encounter weird line breaks like this. To reformat this text, you can use the fmt command to pad the paragraph's lines to a uniform length:

$ fmt trek.txt 
Space: the final frontier. These are the voyages of the starship
Enterprise. Its continuing mission: to explore strange new worlds. To
seek out new life and new civilizations. To boldly go where no one has
gone before!

By default, fmt will format text to a column width size of 75, but you can change this using the -w or –width option:

$ fmt -w 60 trek.txt 
Space: the final frontier. These are the voyages of
the starship Enterprise. Its continuing mission: to
explore strange new worlds. To seek out new life and new
civilizations. To boldly go where no one has gone before!

Format email replies using Linux fmt command

I'm on a mailing list that prefers plain text emails, which makes archiving emails on the list server much easier. But the reality is that not everyone sends emails in plain text. Sometimes when I reply to these emails in plain text, my email client puts the entire paragraph on one line. This makes it difficult to "quote" responses in emails.

This is a simple example. When I reply to emails in plain text, my email client "quotes" the other person's email by adding a > character before each line. For a short message, it might look like this:

> I like the idea of the interim development builds.

Long lines without proper "wrap" will not display correctly in my plain text email reply because it is just long lines preceded by a > character like this:

> I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with.

To fix this, I opened a terminal and copied and pasted the quoted text into a new file. I then use the -p or –prefix option to tell fmt what character to "prefix" before each line.

$ cat > email.txt
> I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with.
^D
$ fmt -p '>' email.txt
> I like the idea of the interim development builds. This should be a
> great way to test new changes that everyone can experiment with.
The

fmt command is a very simple text formatter, but it can do a lot of useful things that can help write and update documents in plain text. Be aware of other options such as -c or –crown-margin to match the indentation of the first two lines of a paragraph, such as a list of items. Also try using -t or –tagged-paragraph to preserve the indentation of the first line in a paragraph, just like an indented paragraph. The -u or –uniform-spacing option uses one space between words and two spaces between sentences.

The above is the detailed content of Format text using Linux fmt. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete