fold linux_如何在Linux上使用fold命令

fold linux

fold linux

A shell prompt on a Linux PC
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

The Linux fold command brings unruly output to heel. Read wide chunks of text, endless strings, and unformatted streams by controlling the width of the output. Learn how.

Linux fold命令带来了混乱的输出。 通过控制输出的宽度来读取大块的文本,无尽的字符串和未格式化的流。 学习如何。

文本行在Linux终端中如何工作 (How Lines of Text Work in the Linux Terminal)

First rule of Linux combat: know your enemy. So let’s define it. What exactly is a line of text? It’s a sequence of characters—letters, numbers, symbols, and whitespace—that is terminated by a special byte that means “start a new line.” In Linux and Unix, the newline character, also called a linefeed, is used as the end of line indicator. This is a byte with a value of 0x0a in hexadecimal and ten in decimal.

Linux作战的第一法则:认识你的敌人。 因此,让我们对其进行定义。 一行文字到底是什么? 它是由字符,字母,数字,符号和空格组成的序列,由一个特殊的字节终止,表示“开始新行”。 在Linux和Unix中, 换行符 (也称为换行符)用作行尾指示符。 这是一个字节, 十六进制值为0x0a,十进制为十。

Different operating systems use different byte values to indicate the end of a line. Windows uses a two-byte sequence. In Windows text files, the newline character is followed immediately by the carriage return character, which is 0x0d in hexadecimal and thirteen in decimal.

不同的操作系统使用不同的字节值来指示行的结尾。 Windows使用两个字节的序列。 在Windows文本文件中,换行符后紧跟回车符 ,十六进制为0x0d,十进制为13。

The terms “linefeed” and “carriage return” date back to the typewriter. The platen, the cylinder that the paper was wrapped around, was mounted on a moveable carriage. The carriage moved one character’s width to the left each time you hit a key. To start a new line, you pushed a lever that brought the carriage back to its original position, and which rotated the roller and moved the paper upwards by the height of one line. This action was known as the carriage return, and the rotation of the cylinder (and the advancement of the paper) was known as a linefeed.

术语“换行”和“回车”可以追溯到打字机 。 压纸卷筒, 即包裹纸张的圆筒 ,被安装在可移动的托架上。 每次您按下键时,笔架都会向左移动一个字符的宽度。 要开始新的生产线,您按下了一个杠杆,使托架回到原来的位置,并旋转了滚轴并使纸张向上移动了一行的高度。 此动作称为回车,而滚筒的旋转(以及进纸)称为换行。

The lever was replaced by a key when the typewriter became electrified. The key was labeled Carriage Return or just Return. Some early computers such as the BBC Micro still used the name Return on what we now call the Enter key.

打字机通电后,用钥匙代替了杠杆。 密钥标记为回车或仅回车。 一些早期的计算机(如BBC Micro)在我们现在称为Enter键的名称上仍使用名称Return

You can’t see newline characters, as a rule. You can only see their effect. The newline character forces software that displays or processes text to start a new line.

通常,您看不到换行符。 您只能看到它们的效果 。 换行符强制显示或处理文本的软件开始换行。

但是长排有什么问题? (But What’s the Problem With Long Lines?)

Text with no, or very few, newline characters in it will be too wide to comfortably read in the terminal window. That’s annoying, but it is do-able.

没有或很少有换行符的文本太宽,无法在终端窗口中舒适地阅读。 这很烦人,但这是可行的。

A more pernicious issue is having to deal with lines of such length that they pose a problem to the software that needs to process, transmit, or receive the text. This might be caused by internal buffer lengths or other aspects of the software that you cannot adjust.

一个更有害的问题是必须处理如此长的行,以至于它们给需要处理,传输或接收文本的软件带来了问题。 这可能是由于内部缓冲区长度或软件无法调整的其他方面引起的。

But there’s a fix for that, called fold.

但是有一个解决方法,叫做fold

折叠的第一步 (First Steps with fold)

Let’s take a look at a portion of text that has very, very long lines in it. Note that we’re not talking about sentences here. (Although the text comes from Herman Melville’s Moby Dick, so we’ve got the best of both worlds.)

让我们看一下其中有非常长的一行的一部分文本。 请注意,我们在这里不是在谈论句子。 (尽管文本来自赫尔曼·梅尔维尔的《白鲸记》,所以我们两全其美。)

A line of text is everything from the last newline character (or the start of the file if it is the first line in the file) all the way up to the next newline character, regardless of what is in between. The line may contain many sentences. It may wrap round in the terminal window many times. But it is still a single line of text.

文本行是从最后一个换行符(或文件的开头,如果它是文件中的第一行)一直到下一个换行符之间的所有内容,而不论中间是什么。 该行可能包含许多句子。 它可能会在终端窗口中多次缠绕。 但它仍然是文本的单行

Let’s look at the text in its raw form:

让我们看一下原始格式的文本:

less moby-dick.txt
less moby-dick.txt in a terminal window

The text is displayed in less:

文本显示为less

Moby Dick displayed in less in a terminal window

The text stretches from one edge of the window to the other, and the line wraps are ugly, and they break words in the middle.

文本从窗口的一个边缘延伸到另一边缘,换行很丑陋,中间的单词断了。

We’ve got another version of the file with short lines:

我们还有另一个带有短行的文件版本:

less short-lines-moby-dick.txt
less short-lines-moby-dick.txt in a terminal window

The lines in this file are much shorter. Each line is terminated with a newline character.

该文件中的行短得多。 每行以换行符终止。

Short line version of Moby Dick displayed in less in a terminal window

If we use the hexdump command, we can look at the byte values within the file and see the newline characters. The -C (canonical) option formats the output to show hexadecimal values in the main body of the display with the text equivalents in a column at the side. We’ll pipe the output into less:

如果使用hexdump命令,我们可以查看文件中的字节值并查看换行符。 -C (规范)选项将输出格式化,以在显示屏的主体中显示十六进制值,并在侧面的一列中显示等效文本。 我们将输出传递到less

hexdump -C short-lines-moby-dick.txt | less
hexdump -C short-lines-moby-dick.txt | less in a terminal window

By pressing the forward slash “/” you’ll enter less‘s search function. Type “0a” and Press Enter. The newline characters will be highlighted in the text. You can scroll through the file and see where they appear. If you need to, you can scroll the output sideways using the Left Arrow and Right Arrow keys.

通过按正斜杠“ / ”,您将输入less的搜索功能。 Enter “ 0a”,然后按Enter 。 换行符将在文本中突出显示。 您可以滚动浏览文件并查看它们的出现位置。 如果需要,可以使用Left ArrowRight Arrow键横向滚动输出。

Output from hexdump in less in a terminal window with newline characters highlighted

Having a newline character at the end of each line can be a limitation in itself. No matter what program or window displays this text, the lines cannot adapt to windows with a width wider than the lines themselves. The line length has been capped by the newline characters.

每行末尾都有换行符可能是一个限制。 无论什么程序或窗口显示此文本,这些行都不能适应宽度比行本身宽的窗口。 行长已由换行符限制。

So there are problems with long lines and short lines alike.

因此,长线和短线都存在问题。

减少长线 (Reducing Long Lines)

The fold command has an option -w (width) that lets you to specify a new maximum width for a section of text. We’ll display the Moby Dick text with a maximum width of 50 characters:

fold命令具有-w (宽度)选项,可用于为一段文本指定新的最大宽度。 我们将显示Moby Dick文本,最大宽度为50个字符:

fold -w 50 moby-dick.txt
fold -w 50 moby-dick.txt in a terminal window

The text is displayed in the terminal window, with the new maximum file length. The original file isn’t changed. It is only the output from fold that is reformatted.

文本以新的最大文件长度显示在终端窗口中。 原始文件未更改。 仅将fold的输出重新格式化。

Output from fold with Moby Dick text displayed at a maximum length of 50 characters, in a terminal window

At first glance, this looks a lot better. But words are still getting split in the middle at the ends of lines. It is definitely easier to read, but some of the awkward word breaks are jarring.

乍一看,这看起来好多了。 但是单词仍然在行尾的中间被拆分。 绝对更容易阅读,但是有些尴尬的断字令人讨厌。

Although it looks like the right-hand margin of the text wavers in and out, all of the line lengths are the same. The lines that appear to be one character shorter than the rest happen to end in a space character.

尽管看起来像文本的右手边空白,但所有行长都相同。 看起来比其他字符短一个字符的行恰好以空格字符结尾。

在空间分割线 (Splitting Lines at Spaces)

We can use the -s (spaces) option to make sure that lines are only split on space characters, and no words are broken across two lines.

我们可以使用-s (空格)选项来确保仅在空格字符上分割行,并且在两行之间没有断行。

fold -w 50 -s moby-dick.txt
fold -w 50 -s moby-dick.txt in a terminal window

The output now has a ragged right-hand margin, but it is easier to read. All of the words finish on the lines they started on.

现在,输出的右侧边距参差不齐,但更易于阅读。 所有的单词都以它们开始的行结束。

Output from fold -w 50 -s moby-dick.txt in a terminal window

使短线更长 (Making Short Lines Longer)

As well as making long lines shorter, we can use fold to remove the enforced line lengths of shorter lines.

除了使长行变短之外,我们还可以使用fold来删除较短行的强制行长。

fold -w 75 short-lines-moby-dick.txt
fold -w 75 short-lines-moby-dick.txt in a terminal window

The newline characters are removed, and the text now wraps on or before the allotted maximum length.

换行符将被删除,并且文本现在在分配的最大长度上或之前自动换行。

Output from a fold -w 75 short-lines-moby-dick.txt in a terminal window

进行永久更改 (Making Changes Permanent)

fold can’t modify the original file. If you want to keep the changes, you’ll have to redirect the output from fold into a new file. We’ll redirect the output into a file called “modified-moby-dick.txt.”

fold无法修改原始文件。 如果要保留更改,则必须将输出从fold重定向到新文件。 我们将输出重定向到一个名为“ modified-moby-dick.txt”的文件中。

fold -w 75 -s short-lines-moby-dick.txt > modified-moby-dick.txt
fold -w 75 -s short-lines-moby-dick.txt > modified-moby-dick.txt in a terminal window

Let’s have a look at our new file:

让我们看看我们的新文件:

less modified-moby-dick.txt
less modified-moby-dick.txt in a terminal window

How does our new file look?

我们的新文件看起来如何?

less modified-moby-dick.txt in a terminal window

The text is now wrapping neatly at our new line width, which is wider than the original file’s line lengths.

现在,文本正好以我们的新行宽换行,新行宽比原始文件的行长宽。

在流中使用折叠 (Using fold With Streams)

We can use fold to reformat streams of text. It is not restricted to working solely with files. Let’s see what the raw output from the journalctl tool looks like. The -f (follow) option shows the newest entries in the systemd journal and updates as new entries arrive.

我们可以使用fold来重新格式化文本流。 它不限于仅使用文件。 让我们看看journalctl工具的原始输出是什么样的。 -f (跟随)选项显示systemd日志中的最新条目, 并随着新条目的到达而更新

sudo journalctl -f
sudo journalctl -f in a terminal window

The output wraps at the terminal window’s edge.

输出环绕在终端窗口的边缘。

Output from sudo journalctl -f in a terminal window

It doesn’t look too bad, but for the sake of demonstration, let’s reduce its width slightly. We’re going to pipe the output from journalctl into fold. We’re setting the maximum width to 65 characters, and we’re breaking the lines on spaces only.

它看起来还不错,但是为了演示起见,让我们稍微减小它的宽度。 我们将把journalctl的输出journalctlfold 。 我们将最大宽度设置为65个字符,并且只中断了空格。

sudo journalctl -f | fold -w 65 -s
sudo journalctl -f | fold -w 65 -s in a terminal window

The display looks a little less overwhelming and a touch neater too.

显示屏看起来不那么压倒性和触摸整洁。

Output from sudo journalctl -f | fold -w 65 -s in a terminal window


Walls of solid text can seem impenetrable. They’re off-putting and sapping to deal with. When you need to be able to see the wood from the trees, call on fold and impose a bit of order.

纯文字的墙似乎无法穿透。 他们令人讨厌并且需要应付。 当您需要能够从树上看到木头时,请fold并施加一些命令。

翻译自: https://www.howtogeek.com/538778/how-to-use-the-fold-command-on-linux/

fold linux

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值