Linux:cutコマンドでデリミタ(区切り文字)を指定する方法

スポンサーリンク

cutコマンドでデリミタ(区切り文字)を指定する方法

cutコマンドでデリミタ(区切り文字)を指定するには-dオプションを指定します。

 

オプションの説明

  -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
  -f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified

 

以下のテキストを処理するとします。

$ cat test.txt
id,lower,upper
1,a,A
2,b,B
3,c,C

 

-dオプションを指定した場合

$ cut -d, -f2 test.txt
lower
a
b
c

 

-dオプションを指定しない場合(デフォルトはタブ区切り)

$ cut -f2 test.txt
id,lower,upper
1,a,A
2,b,B
3,c,C