Using the Linux fold command to make text more readable


The Linux fold takes lines of text and breaks them into chunks based on the arguments that you provide. With no arguments, fold will break lines at 80 characters.

The first example below uses a single-line text file that includes indications of character positions. First, we count the number of characters and lines in the file using the wc -l and wc -l command:

$ wc -c wide_text
251 wide_text
$ wc -l wide_text
1 wide_text

So, this file has 251 characters (including a carriage return) and a single line of text. Next, we display the file using the cat command:

$ cat wide_text
.........1.........2.........3.........4.........5.........6.........7.........8.........9........10........11........12........13........14........15........16........17........18........19........20........21........22........23........24........25

Then we use the fold command to break the file into separate lines:

$ fold wide_text
.........1.........2.........3.........4.........5.........6.........7.........8
.........9........10........11........12........13........14........15........16
........17........18........19........20........21........22........23........24
........25

In the example above, a 250-character line is passed to the fold command, and it generates four lines of text – three with 80 characters and a fourth with the remaining 10.

The fold command does, however, provide the -c (characters) option to break lines into larger or smaller chunks. In the example below, we break the wide_text file contents into 50-character chunks.

$ fold -c50 wide_text
.........1.........2.........3.........4.........5
.........6.........7.........8.........9........10
........11........12........13........14........15
........16........17........18........19........20
........21........22........23........24........25

Folding the text and creating a new file in one step is easy:

$ fold -c50 wide_text folded_text
$ wc -l folded_text; wc -c folded_text
5 folded_text
255 folded_text

As you can see, the folded_text file contains five lines of text and 255 characters. You can also fold lines by bytes instead of characters using the -b option when this is useful. In some cases, this changes the output.

Now let’s look at a more realistic example in which we break a text file with very long lines into more reasonable lines for viewing.

First, to view how many characters are in each line in a file, we use a script like the one shown below. The two if commands at the top check that a file name has been provided and that the specified file exists. The while command at the bottom displays the length of each line in the file.

#!/bin/bash

# check for an argument
if [ $# -eq 0 ]; then
    echo -n  "file: "
    read file
else
    file=$1
fi

# make sure the file exists or exit
if [ ! -f $file ]; then
    echo "ERROR: cannot find $file"
    exit 1
fi

$ count characters in each line
while read line; do
    echo $line | wc -c
done < $file

The script displays the length of each line in the file, clearly more than would display well in a terminal window, so it’s a good candidate for folding.

$ chars-per-line Lorem_ipsum | head -6
7
137
149
153
154
35

If we break this file into 80 characters lines, we may see something like the example below using the centuries old “Lorem ipsum” text:

$ fold Lorem_ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i
ncididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostru
d exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aut
e irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat n
ulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
 officia deserunt mollit anim id est laborum.

As you can see from the example above, breaking text files at a fixed position won’t likely be a good choice since it would probably break some words into two pieces.

When we add the -s (break at spaces) option, we get a much more useful rendering of the text:

$ fold -s Lorem_ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

Note: Text files will be a little larger when folded with the fold command because they will contain more newline (carriage return) characters.

Wrap-Up

The fold command can be very useful when you want to turn paragraphs of text into separate lines that don’t have as many characters and break on word boundaries. This can make viewing a large text a lot easier.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2022 IDG Communications, Inc.



Source link