Chapter 5. Redirection, Pipe and Shell Script

Cut (Extract Data Sections)

Cut (Extract Data Sections)
Tag:

The cut command is used to extract some data sections, which are usually called data fields. This command is useful when the data set already has a consistent structure like data in a table format.

cut command syntax

To explain the cut command, we use the same data file (purchase.txt) as in the previous section.

purchase.txt
274,Theresa,Webb,Apple,6,$19.2
357,Guy,Hawkins,Orange,3,$6.6
703,Devon,Lane,Apple,4,$12.8
994,Arlene,McCoy,Apple,4,$12.8
922,Ralph,Edwards,Bananas,3,$12.6
177,Savannah,Nguyen,Apple,6,$19.2
798,Marvin,McKinney,Grapes,7,$53.2
429,Courtney,Henry,Orange,3,$6.6
492,Floyd,Miles,Bananas,2,$8.4
738,Wade,Warren,Grapes,5,$38
130,Jerome,Bell,Grapes,6,$45.6
185,Esther,Howard,Bananas,4,$16.8
426,Robert,Fox,Bananas,6,$25.2
877,Cameron,Williamson,Apple,7,$22.4
556,Annette,Black,Grapes,4,$30.4

To extract product data from the file, run the command below.

Command Line - INPUT
cut -f 4 -d "," purchase.txt

The result will be like the one below.

Command Line - RESPONSE
Apple
Orange
Apple
Apple
Bananas
Apple
Grapes
:

If you want to save the result in a file, use redirection.

Command Line - INPUT
cut -f 4 -d "," purchase.txt > purchase_product.txt

The cut command is used to extract some data sections, which are usually called data fields. This command is useful when the data set already has a consistent structure like data in a table format.

cut command syntax

To explain the cut command, we use the same data file (purchase.txt) as in the previous section.

purchase.txt
274,Theresa,Webb,Apple,6,$19.2
357,Guy,Hawkins,Orange,3,$6.6
703,Devon,Lane,Apple,4,$12.8
994,Arlene,McCoy,Apple,4,$12.8
922,Ralph,Edwards,Bananas,3,$12.6
177,Savannah,Nguyen,Apple,6,$19.2
798,Marvin,McKinney,Grapes,7,$53.2
429,Courtney,Henry,Orange,3,$6.6
492,Floyd,Miles,Bananas,2,$8.4
738,Wade,Warren,Grapes,5,$38
130,Jerome,Bell,Grapes,6,$45.6
185,Esther,Howard,Bananas,4,$16.8
426,Robert,Fox,Bananas,6,$25.2
877,Cameron,Williamson,Apple,7,$22.4
556,Annette,Black,Grapes,4,$30.4

To extract product data from the file, run the command below.

Command Line - INPUT
cut -f 4 -d "," purchase.txt

The result will be like the one below.

Command Line - RESPONSE
Apple
Orange
Apple
Apple
Bananas
Apple
Grapes
:

If you want to save the result in a file, use redirection.

Command Line - INPUT
cut -f 4 -d "," purchase.txt > purchase_product.txt
Tag: