全力で怠けたい

怠けるために全力を尽くしたいブログ。

入力行を指定幅で折り返す fold コマンドの使い方のメモ。

入力行を指定幅で折り返す fold コマンドの使い方のメモ。

このメモは Ubuntu 20.04.2 LTS の Docker コンテナ上の fold コマンドを使ってる。

Ubuntu, bash, fold コマンドのバージョン。

$ cat /etc/os-release | grep -w VERSION
VERSION="20.04.2 LTS (Focal Fossa)"

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ fold --version
fold (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

あと macOS でデフォルトでインストールされている fold コマンドを使う箇所もある。

macOSbash のバージョン。

$ sw_vers | grep Product
ProductName:    macOS
ProductVersion: 11.2.3

$ bash --version
GNU bash, version 5.1.4(1)-release (x86_64-apple-darwin19.6.0)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

fold コマンドの使い方

fold コマンドは入力行を指定幅で折り返すコマンド。

自分は普段そんなに使わないんだけど圧倒的に不足しているシェル力を養うために シェル芸勉強会 の問題集を解くときに使ったりするので使い方をメモしておく。

--help オプション: ヘルプ

--help オプションを指定すると fold コマンドの使い方を表示する……んだけど man fold が使えないときにしか使わない。

$ fold --help
Usage: fold [OPTION]... [FILE]...
Wrap input lines in each FILE, writing to standard output.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes         count bytes rather than columns
  -s, --spaces        break at spaces
  -w, --width=WIDTH   use WIDTH columns instead of 80
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report fold translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/fold>
or available locally via: info '(coreutils) fold invocation'

基本的な使い方

基本的には fold <ファイル> みたいにして使う。

$ cat message.txt 
0123456789

$ fold -w 2 message.txt 
01
23
45
67
89

fold - みたいに <ファイル> のところを - にするとファイルから読み込むのではなくて標準入力から読み込む。

$ echo '0123456789' | fold -w 2 -
01
23
45
67
89

<ファイル> のところは何も指定しなかったら標準入力を読み込む。 先行するコマンドの出力をパイプで fold コマンドに接続するなら fold - みたいにするよりも fold みたいにしちゃうことが多い気がする。

$ echo '0123456789' | fold -w 2
01
23
45
67
89

-w, --width オプション: 指定幅で折り返す

fold コマンドはデフォルトは 80 の幅で入力を折り返すけど -w オプションか --width オプションを指定すると入力を指定幅で折り返す。 ちなみに macOS にインストールされている BSD 版の fold コマンドはショートオプションの -w オプションはサポートしてるけどロングオプションの --width オプションはサポートしていない。

入力幅が10でデフォルト幅の80に収まるので折り返さない。

$ echo '0123456789' | fold
0123456789

入力幅が10で -w 10 の幅の10に収まるので折り返さない。

$ echo '0123456789' | fold -w 10
0123456789

入力幅が10で -w 9 の幅の9に収まらないので収まらない分の 9 が折り返す。

$ echo '0123456789' | fold -w 9
012345678
9

入力幅が10で -w 3 の幅の3に収まらないので収まらない分の各行がそれぞれ3の幅で折り返す。

$ echo '0123456789' | fold -w 3
012
345
678
9

-s, --spaces オプション

fold コマンドはデフォルトは単に幅で入力を折り返すけど -s オプションか ---spaces オプションを指定すると空白を考慮して折り返す。 英語の文章とかを読むときに便利かもしれない。 ちなみに macOS にインストールされている BSD 版の fold コマンドはショートオプションの -s オプションはサポートしてるけどロングオプションの --spaces オプションはサポートしていない。

入力幅が5で -w 5 の幅の5に収まらないので折り返すけど、 345 の途中と 678 の途中で折り返しちゃう。

$ echo '012 345 678 9' | fold -w 5
012 3
45 67
8 9

入力幅が5で -w 5 の幅の5に収まらないので折り返すけど、 -s を指定してるので 345 の途中では折り返さないで 345 の次の空白で折り返す。678 も同じようになる。

$ echo '012 345 678 9' | fold -w 5 -s
012 
345 
678 9

指定した幅よりも長い単語があるときは単語の途中でも折り返す。

$ echo '012' | fold -w 2 -s
01
2

-b, --bytes オプション: 幅じゃなくてバイト数で折り返す

fold コマンドはデフォルトは幅で入力を折り返すけど -b オプションか ---bytes オプションを指定するとバイト数で折り返す。 ちなみに macOS にインストールされている BSD 版の fold コマンドはショートオプションの -b オプションはサポートしてるけどロングオプションの --bytes オプションはサポートしていない。

タブ文字を含む文字列を fold コマンドで折り返すとこんな感じになる。

$ echo $'01234\t56789' | fold -w 4
0123
4
    
5678
9

-b オプションを指定すると 0123 の4バイトで折り返して $'4\t56' の4バイトで折り返す。

$ echo $'01234\t56789' | fold -w 4 -b
0123
4   56
789

タブ文字はいろいろ扱いにくさがあると思うのでタブそのものに意味がないなら適当にスペースに変換しちゃってもいいと思う。

expand コマンドでタブ文字をスペース1つに展開しちゃうとか。

$ echo $'01234\t56789' | expand -t 1 | fold -w 4
0123
4 56
789

tr コマンドでタブ文字をスペース1つに置換しちゃうとか。

$ echo $'01234\t56789' | tr $'\t' ' ' | fold -w 4
0123
4 56
789

以上。