قالب وردپرس درنا توس
Home / Tips and Tricks / How to use the folding command on Linux

How to use the folding command on Linux

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

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

How text lines work in the Linux terminal

First line of Linux battles: know your enemy. So let's define it. What exactly is a line of text? It is a string of characters (letters, numbers, symbols, and whitespace) that is closed by a special byte, meaning & # 39; starting a new line & # 39 ;. In Linux and Unix, the character for the new line, also called line feed, is used as the end of the line indicator. This is a byte with a value of 0x0a in hexadecimal and ten in decimal.

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

The terms "line feed" and "line return" date from the typewriter. The platen, the cylinder around which the paper was wrapped, was mounted on a movable carriage. Each time you press a button, the carriage moves one character to the left. To start a new line, you pushed a lever that returned the carriage to its original position and turned the roll and moved the paper up a line. This action was known as the carriage return and the rotation of the cylinder (and the progress of the paper) was known as a line feed.

The handle was replaced by a key when the typewriter was electrified. The key was labeled Transport back or Return only. Some early computers, such as the BBC Micro, still used the name Return on what we now call the Enter key .

As a rule, you cannot see any new characters. You can only see their effect . The character with the new line forces software that displays or processes text to start a new line.

But what is the problem with long lines?

Text without or with few new characters is too wide to be read comfortably in the terminal window. That is annoying, but it is feasible.

A more difficult problem is handling lines of such length that they are a problem for the software that must process, send or receive the text. This can be caused by internal buffer lengths or other aspects of the software that you cannot adjust.

But there is a solution for that called fold .

First steps with fold

Let's take a look at a portion of the text with very, very long lines. Note that we are not talking about sentences here. (Although the text comes from Herman Melville's Moby Dick, we have the best of both worlds.)

A line of text is everything from the last newline character (or the beginning of the file if it is the first line) ) in the file) all the way to the next newline character, no matter what is in between. The rule can contain many sentences. It can often wrap itself in the terminal window. 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 less in :

 Moby Dick appears less in a terminal window

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

We have another version of the file with short lines:

  fewer short lines-moby-dick.txt [19659025] fewer short lines-moby-dick.txt in a terminal window " width="644" height="55" src="/pagespeed_static/1.JiBnMqyl6S.gif" onload="pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);" onerror="this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);"/> 

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

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

If we use the command hexdump we can look at the byte values ​​in the file and see the characters for the new line. The -C (canonical) option formats the output with hexadecimal values ​​in the body of the screen with the text equivalents in a side column. We send the output to less :

  hexdump -C short-lines-moby-dick.txt | less 

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

By pressing the slash " / ", you enter less the search function. Type "0a" and press Enter . The characters for the new line are highlighted in the text. You can browse the file and see where they appear. If necessary, you can move the output sideways with the Left arrow and Right arrow keys.

 Output of hex dump in less in a terminal window with newline characters marked

Having a new sign at the end of each line can be a limitation in itself. Regardless of which program or window displays this text, the lines cannot be adjusted to windows that are wider than the lines themselves. The length of the line is capped by the characters of the new line.

So there are problems with both long and short lines.

Reduce long lines

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

  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 has not been changed. It is only the output of fold that is reformatted.

 Output of 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 split in the middle at the end of lines. It is certainly easier to read, but some of the tricky word breaks are shocking.

Although the right margin of the text seems to be waving in and out, all line lengths are the same. The lines that appear to be one character shorter than the rest happen to end up in a space.

Splitting lines on spaces

We can use the option -s (spaces) to ensure that lines are only split on spaces and no words are split between two lines.

  fold -w 50 -s moby-dick.txt 

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

The output now has an uneven right margin, but is more readable. All words end with the lines they started with.

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

Also make short lines longer

if making long lines shorter, we can fold to remove the enforced line lengths from shorter lines.

  fold -w 75 short-lines-moby-dick.txt 

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

The characters for the new line are deleted and the text now runs on or before the assigned maximum length.

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

Making permanent changes

fold cannot change the original file . If you want to keep the changes, you must redirect the output of fold to a new file. We will redirect the output to a file named "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 "width =" 646 "height =" 77 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "/> </p>
<p>  Let's look at our new file: [196518] -moby-dick.txt </p>
<p><img class=

What does our new file look like?

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

The text is now neatly wrapped with our new line width, which is wider than the line lengths of the original file.

Using folds with streams

We can use to reformat text streams. It is not limited to single file work with files. Let's see how the unprocessed output of the tool journalctl The option -f (follow) shows the newest items in the magazine systemd and is updated when new items arrive.

  sudo journalctl -f 

  sudo journa lctl -f in a terminal window

The exit runs at the edge of the terminal window.

 Output of sudo journalctl -f in a terminal window

It does not look too bad, but for the demonstration let's reduce the width slightly. We are going to export journalctl in fold . We set the maximum width to 65 characters and only break the rules for spaces.

  sudo journalctl -f | fold -w 65 -s 

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

The screen looks slightly less overwhelming and is also slightly tidier.

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

Walls with plain text may appear impenetrable. They are unpleasant and juices to deal with. If you need to be able to see the wood for the trees, call on fold and place a little order.

Source link