如何在Linux中將每一行輸入文字換行以適應指定的寬度?


為了將輸入檔案中的長行換行以適應指定的寬度,我們使用Linux作業系統中的fold命令。

fold命令用於透過在Linux系統中執行換行操作,使具有長行的檔案在有限寬度輸出下更易於閱讀。大多數Linux/Unix終端的預設螢幕寬度為80列,有時閱讀長行檔案可能會很麻煩。

fold命令將每行輸入文字換行以適應指定的寬度,並允許使用者設定行的最大長度。fold命令繼承自POSIX的第一個版本。

語法

fold命令的通用語法如下所示。

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

fold命令中可用選項的簡要說明。

序號選項及說明
1-b, --bytes
按位元組計數而不是按列計數
2-c, --characters
按字元計數而不是按列計數
3-s, --spaces
在空格處換行
4-w, --width=WIDTH
使用WIDTH列而不是80列
5--help
顯示此幫助資訊並退出
6--version
輸出版本資訊並退出

預設情況下,fold命令在Linux系統中將行換行到80列,如下所示。

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.

要根據指定的寬度換行檔案中的行,我們使用-w選項和fold命令,如下所示。

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

我們也可以使用Linux系統中的fold命令從標準輸入而不是檔案換行,如下所示。

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

要檢查有關fold命令的更多資訊,我們使用--help選項和fold命令在Linux作業系統中,如下所示。

$ fold --help

要檢查fold命令的版本資訊,我們使用--version選項和fold命令在Linux作業系統中,如下所示。

$ fold --version

更新於:2021年7月1日

616 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.