How to use Linux fold command in bash script

The fold command can be very useful in many scenarios. It allows you to break up text and then use it in a loop. In this post, we will review the fold command and see how it can be used in a bash script and as command argument to format text.

The basic use of the fold command is to take long lines of text and break them into shorter pieces. One common use is to shorten lines in a text file so that they display well in a terminal window. Lines wider than the terminal width might otherwise wrap in inconvenient places.

The fold command can also be used to create a narrower formatted file from a file with lines that are inconveniently long.
$ fold wide.txt > narrow.txt

If a text file contained a single line with 346 characters including the final newline like that shown below, it would wrap, but not necessarily in a way that would look right.

$ cat khs
Kendra Harry-Stocker has been administering Unix systems for more than 30 years.
 She describes herself as “USL” (Unix as a second language) but remembers enough       
 English to write books and buy groceries. She lives in the mountains in Virgini
a where, when not working with or writing about Unix, she’s chasing the bears aw
ay from her bird feeders.

If you were to use the fold command with no options, it would look the same—breaking by default at the end of each 80-character chunk even if that position were in the middle of a word.

$ fold khs
Kendra Harry-Stocker has been administering Unix systems for more than 30 years.
 She describes herself as “USL” (Unix as a second language) but remembers enough       
 English to write books and buy groceries. She lives in the mountains in Virgini
a where, when not working with or writing about Unix, she’s chasing the bears aw ay from her bird feeders.

However, if we add the -s (space) option, the file would be folded on the last blank in each 80-character chunk, so the displayed file would look as it should.

$ fold -s khs
Kendra Harry-Stocker has been administering Unix systems for more than 30
years. She describes herself as “USL” (Unix as a second language) but remembers
enough English to write books and buy groceries. She lives in the mountains in
Virginia where, when not working with or writing about Unix, she’s chasing the
bears away from her bird feeders.

Note that the fold command defaults to 80 characters even if your terminal window is narrower or wider. If you use the -w (width) option along with the -s (space) option, you can display the text properly but in a more narrow display.

$ fold -w 40 -s khs
Kendra Harry-Stocker has been
administering Unix systems for more
than 30 years. She describes herself as
“USL” (Unix as a second language) but
remembers enough English to write books
and buy groceries. She lives in the
mountains in Virginia where, when not
working with or writing about Unix,
she’s chasing the bears away from her
bird feeders.

Strings can be broken into as few letters as you like. If you wanted to break the alphabet into 7-character chunks, you could do something like this:

$ echo abcdefghijklmnopqrstuvwxyz | fold -c7
abcdefg
hijklmn
opqrstu
vwxyz

If you want to loop through a sequence of characters in order to do something with each of them, you can use the fold command to process the characters one at a time. The script below runs through primary vowels (not “y”) in both upper- and lowercase and removes them one at a time from whatever string is entered by the user.

#!/bin/bash

echo -n “Enter a phrase> “
read phrase

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

echo $phrase

Here’s an example of how it runs:

$ rmVowels
Enter a phrase> Every day is another chance to start over
vry dy s nthr chnc t strt vr

If you want to loop through the days of the week, you could do something like this:

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

Conclusion:

Any time when looping through a series of values required, you might consider using a fold command which can ease the task and nicely format the text.

Ref: https://www.networkworld.com/article/3666028/the-linux-fold-command-breaks-up-text-drives-loops.html

Comments

Popular posts from this blog

How To Add Print Button to Blogger Posts

INSTALL CISCO VPN CLIENT ON WINDOWS 10 (32 & 64 BIT). FIX REASON 442