文字列を、指定の文字数ごとに改行させたい時は、nkfコマンドが使えます。
一行に、ずらずらーっと書かれた文字列を指定文字数ごとに改行して整形したりする時、例えばメールの本文は70文字ごとに折り返しましょう的な。
使い方は、以下のようにnkfコマンドの"-f"オプションを利用します。ちゃんと、禁則処理も考慮してくれます。
# "-f"オプションのデフォルトは60文字となります。
使い方は、こんな感じです。
例えば、以下は60文字おきに改行させる場合。
$ nkf -f60 test.txt The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows NT. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards.
このようにnkfコマンドは、禁則処理も考慮してくれるため、英単語の途中で折り返さないような配慮もしてくれます。
ただし、デフォルトでは、マージンは指定の長さ+10文字分だけです。
強制的に60文字で改行させたい場合は、このマージンを"0"に指定すれば良いです。指定の仕方は以下のように、"-f"オプションで文字数を指定した直後に、"-"(ハイフン)をつけてマージンの文字数を渡す感じです。。
$ nkf -f60-0 test.txt The Apache HTTP Server Project is an effort to develop and m aintain an open-source HTTP server for modern operating syst ems including UNIX and Windows NT. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standar ds.
日本語も使えます
ただし、"-f"オプションのあとは、バイト数を指定するイメージですかね。
$ nkf -f10 test.txt あいうえお かきくけこ さしすせそ たちつてと なにぬねの $ nkf -f20 test.txt あいうえおかきくけこ さしすせそたちつてと なにぬねの $ nkf -f15 test.txt あいうえおかき くけこさしすせ そたちつてとな にぬねの

- 作者: Daniel J. Barrett,猪俣敦夫,田淵貴昭,岡本健
- 出版社/メーカー: オライリージャパン
- 発売日: 2005/08/27
- メディア: 単行本(ソフトカバー)
- クリック: 2回
- この商品を含むブログ (5件) を見る