Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

UNIX | cut 入力から特定の文字範囲、列を取り出す

概要

cut 入力から特定の文字範囲、列を取り出す

コマンド

cut [オプション]

オプション

-b, --bytes=LIST        バイトで数えた LIST を選択する
-c, --characters=LIST   文字で数えた LIST を選択する
-d, --delimiter=DELIM   フィールドの区切り文字として TAB の代わりに DELIM
                          を使用する
-f, --fields=LIST       LIST のフィールドのみを選択する。-s オプションが
                          指定されない限り、区切り文字を含まない行も表示
                          する
-n                      (無視される)
    --complement        選択されたバイト数、文字数またはフィールド数の
                          組を補足する
-s, --only-delimited    区切り文字を含まない行を出力しない
    --output-delimiter=STRING  出力の区切り文字として STRING を使用
                          デフォルトでは入力の区切り文字を使用
    --help     この使い方を表示して終了する
    --version  バージョン情報を表示して終了する

サンプル

入力ファイル確認

$more hoge.txt
one,test1
two,test2
three,test3
four,test4
five

文字数で切り出し

$cut -c 5-7 hoge.txt
tes
tes
e,t
,te

区切り文字で切り出し(1項目目)

$cut -d',' -f1 hoge.txt
one
two
three
four
five

区切り文字で切り出し。区切り文字のない行は出力しない(2項目目)

$cut -d',' -f2 -s hoge.txt -n
test1
test2
test3
test4