Saturday, July 2, 2022
HomeNetworkingThe Linux fold command breaks up textual content, drives loops

The Linux fold command breaks up textual content, drives loops


The Linux fold command lets you break a string of characters into same-size chunks, but it surely can be used to offer a sequence of characters or strings to drive a loop. This submit evaluations the essential command after which demonstrates how you should utilize it to loop by the characters or strings that it creates.

The fundamental use of the fold command is to take lengthy traces of textual content and break them into shorter items. One frequent use is to shorten traces in a textual content file in order that they show nicely in a terminal window. Strains wider than the terminal width would possibly in any other case wrap in inconvenient locations.

The fold command can be used to create a narrower file from a file with traces which can be inconveniently lengthy.

$ fold huge.txt > slim.txt

If a textual content file contained a single line with 346 characters together with the ultimate newline like that proven under, it could wrap, however not essentially in a manner that may look proper.

$ cat shs
Sandra Henry-Stocker has been administering Unix programs for greater than 30 years.
 She describes herself as “USL” (Unix as a second language) however remembers sufficient       
 English to jot down books and purchase groceries. She lives within the mountains in Virgini
a the place, when not working with or writing about Unix, she’s chasing the bears aw
ay from her chook feeders.

In case you had been to make use of the fold command with no choices, it could look the identical—breaking by default on the finish of every 80-character chunk even when that place had been in the course of a phrase.

$ fold shs
Sandra Henry-Stocker has been administering Unix programs for greater than 30 years.
 She describes herself as “USL” (Unix as a second language) however remembers sufficient       
 English to jot down books and purchase groceries. She lives within the mountains in Virgini
a the place, when not working with or writing about Unix, she’s chasing the bears aw ay from her chook feeders.

Nonetheless, if we add the -s (house) choice, the file could be folded on the final clean in every 80-character chunk, so the displayed file would look because it ought to.

$ fold -s shs
Sandra Henry-Stocker has been administering Unix programs for greater than 30
years. She describes herself as “USL” (Unix as a second language) however remembers
sufficient English to jot down books and purchase groceries. She lives within the mountains in
Virginia the place, when not working with or writing about Unix, she’s chasing the
bears away from her chook feeders.

Word that the fold command defaults to 80 characters even when your terminal window is narrower or wider. In case you use the -w (width) choice together with the -s (house) choice, you’ll be able to show the textual content correctly however in a extra slim show.

$ fold -w 40 -s shs
Sandra Henry-Stocker has been
administering Unix programs for extra
than 30 years. She describes herself as
“USL” (Unix as a second language) however
remembers sufficient English to jot down books
and purchase groceries. She lives within the
mountains in Virginia the place, when not
working with or writing about Unix,
she’s chasing the bears away from her
chook feeders.

Strings will be damaged into as few letters as you want. In case you wished to interrupt the alphabet into 7-character chunks, you may do one thing like this:

$ echo abcdefghijklmnopqrstuvwxyz | fold -c7
abcdefg
hijklmn
opqrstu
vwxyz

If you wish to loop by a sequence of characters with a purpose to do one thing with every of them, you should utilize the fold command to course of the characters one after the other. The script under runs by major vowels (not “y”) in each upper- and lowercase and removes them one after the other from no matter string is entered by the consumer.

#!/bin/bash

echo -n “Enter a phrase> “
learn phrase

for letter in `echo AEIOUaeiou | fold -w1`
do
    phrase=`echo $phrase | sed “s/$letter//g”`
performed

echo $phrase

Right here’s an instance of the way it runs:

$ rmVowels
Enter a phrase> Day by day is one other likelihood to begin over
vry dy s nthr chnc t strt vr

If you wish to loop by the times of the week, you may do one thing like this:

$ for day in `echo SunMonTueWedThuFriSat | fold -w3`
do
    echo $day
performed
Solar
Mon
Tue
Wed
Thu
Fri
Sat

Wrap-Up

Any time you could loop by a sequence of values that isn’t simply derived from different Linux instructions, you would possibly think about using a cleverly crafted fold command.

Be part of the Community World communities on Fb and LinkedIn to touch upon subjects which can be high of thoughts.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments