Hack 25. Cut コマンド

テキストファイルまたは他のコマンド出力から指定したカラムのみを表示するために Cut コマンドを使用できます。
次はいくつかの例です。

コロン区切りのファイルから第1フィールド (employee name) を表示します。

$ cut -d: -f 1 names.txt
Emma Thomas
Alex Jason
Madison Randy
Sanjay Gupta
Nisha Singh

コロン区切りのファイルから第1と第3フィールドを表示します。

$ cut -d: -f 1,3 names.txt
Emma Thomas:Marketing
Alex Jason:Sales
Madison Randy:Product Development
Sanjay Gupta:Support
Nisha Singh:Sales

ファイル内の行毎の先頭8文字のみ表示します。

$ cut -c 1-8 names.txt Emma Tho
Alex Jas
Madison
Sanjay G
Nisha Si

Cut コマンドの例
o cut -d: -f1 /etc/passwd システムのすべてのユーザの unix ログイン名を表示します。
o free | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2 システムで利用可能な総メモリを表示します。