Linux fmt command usage and Cases
Sometimes you will find that you need to format the content in a text file. For example, the text file has one word per line, and the task is to put all words in the same line. Of course, you can do it manually, but no one prefers to do the time-consuming work manually. In addition, this is just an example-in fact, the task may be amazing.
Fortunately, there is a command that can meet at least part of the text formatting requirements. This tool isfmt
. This tutorial will discussfmt
And some of its main functions. All the commands and commands in this article are tested in Ubuntu 16.04LTS.
Linux fmt command
fmt
A command is a simple text formatting tool that anyone can run on the command line. Its basic syntax is:
fmt [-WIDTH][OPTION]...[FILE]...
The man page is like this:
Reformat each section in the file and write the result to the standard output. Option-WIDTH
Yes--width=DIGITS
Abbreviation of form.
The examples of the following Q & A methods should allow youfmt
Have a good understanding of the usage.
Q1. How to Use fmt to format text content into the same line?
Usefmt
The basic form of the command (omitted any option) can achieve this. You only need to pass the file name as a parameter to it.
fmt [file-name]
The following screenshot shows the command execution result:
Format contents of file in single line
You can see that multiple lines in the file are formatted into the same line. Note that this does not modify the original file (file1 ).
Q2. How do I modify the maximum width of a row?
By default,fmt
The maximum width of the output line is 75. However, you can use-w
Option. It accepts a number indicating the width of the new line as the parameter value.
fmt -w[n][file-name]
In the following example, the row width is reduced to 20:
Change maximum line width
Q3. How can fmt highlight the first line?
This is achieved by making the indentation of the first line different. You can use-t
.
fmt -t [file-name]
Make fmt highlight the first line
Q4. How to Use fmt to split long rows?
The fmt command can also be used to split long lines. You can use-s
Option to apply this function.
fmt -s [file-name]
The following is an example:
Make fmt split long lines
Q5. How can sentences be separated by spaces between words?
The fmt command provides-u
This will separate words with a single space, and separate sentences with two spaces. You can use:
fmt -u [file-name]
Note: In our case, this function is enabled by default.
Summary
Yes,fmt
There are not many functions provided, but it does not mean that it is not widely used. Because you never know when to use it. In this tutorial, we have explainedfmt
Provides the main options. For more details, see the man page of the tool.
Via: https://www.howtoforge.com/linux-fmt-command/
Author: Himanshu Arora Translator: lujun9972 Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China