How to wrap each input line to fit in specified width in Linux?

LinuxOperating SystemOpen Source

To wraps a line in an input file to fit the specified width, we use the fold command in the Linux operating system.

The fold command is used to making a file with long lines more readable on a limited width output by performing a line wrap in the Linux system. Most Linux/Unix terminals default screen width of 80, and sometimes while reading files with long lines could be annoying.

The fold command wraps each input line to fit in specified width and allows the user to set the maximum length of a line. The fold command was inherited into the first version of POSIX.

Syntax

The general syntax of the fold command is as follows.

fold [OPTION]... [FILE]...

Brief description of options available in the fold command.

Sr.No.Option & Description
1-b, --bytes
Count bytes instead of columns
2-c, --characters
Count characters instead of count columns
3-s, --spaces
Break at spaces
4-w, --width=WIDTH
Use WIDTH columns instead of 80
5--help
Display this help and exit
6--version
Output version information and exit

By default, the fold command wraps the line up to 80 columns in the Linux system as shown below.

vikash@tutorialspoint− ~$ cat text.txt
You can decide what you are going to think in any given situation. Your thoughts
and feelings determine your actions and determine the results you get. It all
starts with your thoughts – and I have found that inspirational words are a quick
way to retune you’re thinking.
vikash@tutorialspoint− ~$ fold text.txt
You can decide what you are going to think in any given situation. Your thoughts
and feelings determine your actions and determine the results you get. It all
starts with your thoughts – and I have found that inspirational words are a quick
way to retune you’re thinking.

To wrap the line of a file by specified width, we use the -w option with the fold command as shown below.

$ cat text.txt
Hey, welcome to tutorialspoint...
$ fold -w3 text.txt
Hey
, w
Elc
Ome
To
Tu
Tor
Spo
int
...

We can also wrap the line from standard input instead of file by using the fold command in the Linux system as shown below.

$ fold -w5
Hey, welcome to Tutorialspoint...
Hey,
Welcome
To
Tutorialspoint...

To check more information about the fold command, we use the --help option with the fold command in the Linux operating system as shown below.

$ fold --help

To check version information of the fold command, we use the --version option with the fold command in the Linux operating system as shown below.

$ fold --version
raja
Published on 01-Jul-2021 15:45:37
Advertisements