Linux fmt command usage and Cases

Source: Internet
Author: User

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 discussfmtAnd some of its main functions. All the commands and commands in this article are tested in Ubuntu 16.04LTS.

 

Linux fmt command

fmtA command is a simple text formatting tool that anyone can run on the command line. Its basic syntax is:

  1. 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-WIDTHYes--width=DIGITSAbbreviation of form.

The examples of the following Q & A methods should allow youfmtHave a good understanding of the usage.

 

Q1. How to Use fmt to format text content into the same line?

UsefmtThe basic form of the command (omitted any option) can achieve this. You only need to pass the file name as a parameter to it.

  1. 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,fmtThe maximum width of the output line is 75. However, you can use-wOption. It accepts a number indicating the width of the new line as the parameter value.

  1. 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.

  1. 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-sOption to apply this function.

  1. 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-uThis will separate words with a single space, and separate sentences with two spaces. You can use:

  1. fmt -u [file-name]

Note: In our case, this function is enabled by default.

 

Summary

Yes,fmtThere 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 explainedfmtProvides 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.