Linux:cutコマンドで指定したカラム(フィールド)以外を表示する方法

スポンサーリンク

cutコマンドで指定したカラム(フィールド)以外を表示する方法

cutコマンドで指定したカラム(フィールド)以外を表示するには--complementオプションを指定します。

 

オプションの説明

  -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
      --complement        complement the set of selected bytes, characters
                            or fields

 

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

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

 

--complementオプションを指定した場合(-dはデリミタを指定するオプション)

$ cut --complement -d, -f2 test.txt
id,upper
1,A
2,B
3,C

 

--complementオプションを指定しない場合

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