coreutilsを使用したテキストの折り返しとインデント

coreutilsを使用したテキストの折り返しとインデント

簡潔なバージョン

次のように、複数行のテキストを表形式で表示したいと思います。

all       Build all targets
document  Create documentation of source files in the subfolders
          `src` and `script`, and write it to `man`
test      Run unit tests

現在、これに対する私の入力は次のようになりますが、もちろん変更できます。

all---Build all targets
document---Create documentation of source files in the subfolders `src` and `script`, and write it to `man`
test---Run unit tests

これを達成するために、およびawk/の組み合わせを試しましたが、行が途切れている間にインデントは機能しません。これが私の現在のアプローチです。wrappr


| awk -F '---' "{ printf '%-10s %s\n', $1, $2 }" \
| fold -w $(($COLUMNS - 1)) -s

出力を生成します

all       Build all targets
document  Create documentation of source files in the subfolders
`src` and `script`, and write it to `man`
test      Run unit tests

...つまり、3行目は期待どおりにインデントされませんでした。

与えられた改行の長さと与えられたインデント幅でテキストをフォーマットする方法は?- テキストの他の内容は変更しません。ボーナス:UTF-8およびエスケープ/制御文字と連携する必要があります。


背景情報

作ることが目標です自己文書化 Makefile。したがって、コードをフォーマットして表示するためのロジックは小さく独立している必要があり、個別にインストールされたソフトウェアに依存してはいけません。理想的には、Makefileを実行できるすべてのシステムで動作する必要があるため、coreutilsの制限はより近いです。

つまり、私は以下をgroff使ってgroff問題を簡単に解決しました。

これ生の文字列私は次のように解析してフォーマットしようとしています。

## Build all targets
all: test document

## Run unit tests
test:
    ./run-tests .

## create documentation of source files in the subfolders `src` and `script`,
## and write it to `man`
document:
    ${MAKE} -C src document
    ${MAKE} -C script document

現在、これはsed上記の書式設定コードにコメントを提供する前に複数行のコメントを無視するスクリプト(詳細についてはリンクを参照)を使用して解析されます。

ベストアンサー1

折りたたみコマンドの後、出力をsedにパイプし、行の先頭をタブに置き換えます。以前の「tabs」コマンドを使用してインデントを制御できます。

tabs 5
echo "A very long line that I want to fold on the word boundary and indent as well" | fold -s -w 20  | sed -e "s|^|\t|g"
     非常に長い行
     折りたい
     この言葉について
     ボーダーとインデント
     しかも

おすすめ記事