この切り取りコマンドに-dオプションが必要なのはなぜですか?

この切り取りコマンドに-dオプションが必要なのはなぜですか?

sales.txtというtxtファイルがあります。

Fred apples 20 April 4
Susy oranges 5 April 7
Mark watermelons 12 April 10
Terry peaches 7 April 15

このコマンドを使用するとき:

[root@ip-10-0-7-125 bash-tut]# cat sales.txt | cat /dev/stdin | cut -d' ' -f 2,3 | sort
20 April
oranges 5
peaches 7
watermelons 12

要点は、-d ''部分を削除すると、テキストファイルのすべてのフィールドが取得されることです。

[root@ip-10-0-7-125 bash-tut]# cat sales.txt | cat /dev/stdin | cut -f 2,3 | sort
Mark watermelons 12 April 10
pples 20 April 4
Susy oranges 5 April 7
Terry peaches 7 April 15
[root@ip-10-0-7-125 bash-tut]# man cut

なぜこれが起こるのですか?マニュアルページでdオプションを探すと、次のようになります。 -d はフィールド区切り文字を意味します。

ベストアンサー1

私のシステムのマニュアルページには次のように記載されています。

   -d, --delimiter=DELIM
          use DELIM instead of TAB for field delimiter

したがって、これを指定しないと、-dフィールドはcut文字で区切られたと見なされます。TAB入力ファイルにTAB文字が含まれていません。また、マニュアルページには次のように記載されています。

   -f, --fields=LIST
          select only these fields;  also print any line that contains  no
          delimiter character, unless the -s option is specified

重要な部分は、「区切り文字を含まないすべての行も印刷します」です。これがあなたが持っているものです:ファイルのすべての行には「区切り記号なし」が含まれています。

おすすめ記事