From 199c553b55b77dac63e92f55e7e9a1ef99193e2d Mon Sep 17 00:00:00 2001 From: darksun Date: Wed, 10 Jan 2018 10:09:31 +0800 Subject: [PATCH] translate done: 20170918 Linux fmt command - usage and examples.md --- ... Linux fmt command - usage and examples.md | 93 --------------- ... Linux fmt command - usage and examples.md | 106 ++++++++++++++++++ 2 files changed, 106 insertions(+), 93 deletions(-) delete mode 100644 sources/tech/20170918 Linux fmt command - usage and examples.md create mode 100644 translated/tech/20170918 Linux fmt command - usage and examples.md diff --git a/sources/tech/20170918 Linux fmt command - usage and examples.md b/sources/tech/20170918 Linux fmt command - usage and examples.md deleted file mode 100644 index 19d0452d96..0000000000 --- a/sources/tech/20170918 Linux fmt command - usage and examples.md +++ /dev/null @@ -1,93 +0,0 @@ -translating by lujun9972 -Linux fmt command - usage and examples -====== - -Sometimes you may find yourself in a situation where-in the requirement is to format the contents of a text file. For example, the text file contains one word per line, and the task is to format all the words in a single line. Of course, this can be done manually, but not everyone likes doing time consuming stuff manually. Plus, that's just one use-case - the requirement could be anything. - -Gladly, there exists a command that can cater to at-least some of the text formatting requirements. The tool in question is dubbed **fmt**. In this tutorial, we will discuss the basics of fmt, as well as some of main features it provides. Please note that all commands and instructions mentioned here have been tested on Ubuntu 16.04LTS. - -### Linux fmt command - -The fmt command is a simple text formatting tool available to users of the Linux command line. Following is its basic syntax: - -fmt [-WIDTH] [OPTION]... [FILE]... - -And here's how the man page describes it: - -Reformat each paragraph in the FILE(s), writing to standard output. The option -WIDTH is an abbreviated form of --width=DIGITS. - -Following are some Q&A-styled examples that should give you a good idea about fmt's usage. - -### Q1. How to format contents of file in single line using fmt? - -That's what the fmt command does when used in its basic form (sans any options). You only need to pass the filename as an argument. - -fmt [file-name] - -The following screenshot shows the command in action: - -[![format contents of file in single line][1]][2] - -So you can see that multiple lines in the file were formatted in a way that everything got clubbed up in a single line. Please note that the original file (file1 in this case) remains unaffected. - -### Q2. How to change maximum line width? - -By default, the maximum width of a line that fmt command produces in output is 75. However, if you want, you can change that using the **-w** command line option, which requires a numerical value representing the new limit. - -fmt -w [n] [file-name] - -Here's an example where width was reduced to 20: - -[![change maximum line width][3]][4] - -### Q3. How to make fmt highlight the first line? - -This can be done by making the indentation of the first line different from the rest, something which you can do by using the **-t** command line option. - -fmt -t [file-name] - -[![make fmt highlight the first line][5]][6] - -### Q4. How to make fmt split long lines? - -The fmt command is capable of splitting long lines as well, a feature which you can access using the **-s** command line option. - -fmt -s [file-name] - -Here's an example of this option: - -[![make fmt split long lines][7]][8] - -### Q5. How to have separate spacing for words and lines? - -The fmt command offers a **-u** option, which ensures one space between words and two between sentences. Here's how you can use it: - -fmt -u [file-name] - -Note that this feature was enabled by default in our case. - -### Conclusion - -Agreed, fmt offers limited features, but you can't say it has limited audience. Reason being, you never know when you may need it. Here, in this tutorial, we've covered majority of the command line options that fmt offers. For more details, head to the tool's [man page][9]. - - --------------------------------------------------------------------------------- - -via: https://www.howtoforge.com/linux-fmt-command/ - -作者:[Himanshu Arora][a] -译者:[lujun9972](https://github.com/lujun9972) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:https://www.howtoforge.com -[1]:https://www.howtoforge.com/images/linux_fmt_command/fmt-basic-usage.png -[2]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-basic-usage.png -[3]:https://www.howtoforge.com/images/linux_fmt_command/fmt-w-option.png -[4]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-w-option.png -[5]:https://www.howtoforge.com/images/linux_fmt_command/fmt-t-option.png -[6]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-t-option.png -[7]:https://www.howtoforge.com/images/linux_fmt_command/fmt-s-option.png -[8]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-s-option.png -[9]:https://linux.die.net/man/1/fmt diff --git a/translated/tech/20170918 Linux fmt command - usage and examples.md b/translated/tech/20170918 Linux fmt command - usage and examples.md new file mode 100644 index 0000000000..e9b1d8921a --- /dev/null +++ b/translated/tech/20170918 Linux fmt command - usage and examples.md @@ -0,0 +1,106 @@ +Linux fmt 命令 - 用法与案例 +====== + +有时你会发现需要格式化某个文本文件中的内容。比如,该文本文件每行一个单词,而人物是把所有的单词都放在同一行。当然,你可以手工来做,但没人喜欢手工做这么耗时的工作。而且,这只是一个例子 - 事实上的任务可能千奇百怪。 + +好在,有一个命令可以满足至少一部分的文本格式化的需求。这个工具就是 `fmt`。本教程将会讨论 `fmt` 的基本用法以及它提供的一些主要功能。文中所有的命令和指令都在 Ubuntu 16.04LTS 下经过了测试。 + +### Linux fmt 命令 + +fmt 命令是一个简单的文本格式化工具,任何人都能在命令行下运行它。它的基本语法为: + +``` +fmt [-WIDTH] [OPTION]... [FILE]... +``` + +它的 man 页是这么说的: + +``` +重新格式化文件FILE(s)中的每一个段落,将结果写到标准输出. 选项 -WIDTH 是 --width=DIGITS 形式的缩写 +``` + +下面这些问答方式的例子应该能让你对 fmt 的用法有很好的了解。 + +### Q1。如何使用 fmt 来将文本内容格式成同一行? + +使用 `fmt` 命令的基本格式(省略任何选项)就能做到这一点。你只需要将文件名作为参数传递给它。 + +``` +fmt [file-name] +``` + +下面截屏是命令的执行结果: + +[![format contents of file in single line][1]][2] + +你可以看到文件中多行内容都被格式化成同一行了。请注意,这并不会修改原文件(也就是 file1)。 + +### Q2。如何修改最大行宽? + +默认情况下,`fmt` 命令产生的输出中的最大行宽为 75。然而,如果你想的话,可以用 `-w` 选项进行修改,它接受一个表示新行宽的数字作为参数值。 + +``` +fmt -w [n] [file-name] +``` + +下面这个例子把行宽削减到了 20: + +[![change maximum line width][3]][4] + +### Q3。如何让 fmt 突出显示第一行? + +这是通过让第一行的缩进与众不同来实现的,你可以使用 `-t` 选项来实现。 + +``` +fmt -t [file-name] +``` + +[![make fmt highlight the first line][5]][6] + +### Q4。如何使用 fmt 拆分长行? + +fmt 命令也能用来对长行进行拆分,你可以使用 `-s` 选项来应用该功能。 + +``` +fmt -s [file-name] +``` + +下面是一个例子: + +[![make fmt split long lines][7]][8] + +### Q5。如何在单词与单词之间,行与行之间用空格分开? + +fmt 命令提供了一个 `-u` 选项,这会在单词与单词之间用单个空格分开,行与行之间用两个空格分开。你可以这样用: + +``` +fmt -u [file-name] +``` + +注意,在我们的案例中,这个功能是默认开启的。 + +### 总结 + +没错,fmt 提供的功能不多,但不代表它的应用就不广泛。因为你永远不知道什么时候会用到它。在本教程中,我们已经讲解了 `fmt` 提供的主要选项。若想了解更多细节,请查看该工具的 [man 页 ][9]。 + + +-------------------------------------------------------------------------------- + +via: https://www.howtoforge.com/linux-fmt-command/ + +作者:[Himanshu Arora][a] +译者:[lujun9972](https://github.com/lujun9972) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]:https://www.howtoforge.com +[1]:https://www.howtoforge.com/images/linux_fmt_command/fmt-basic-usage.png +[2]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-basic-usage.png +[3]:https://www.howtoforge.com/images/linux_fmt_command/fmt-w-option.png +[4]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-w-option.png +[5]:https://www.howtoforge.com/images/linux_fmt_command/fmt-t-option.png +[6]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-t-option.png +[7]:https://www.howtoforge.com/images/linux_fmt_command/fmt-s-option.png +[8]:https://www.howtoforge.com/images/linux_fmt_command/big/fmt-s-option.png +[9]:https://linux.die.net/man/1/fmt