Prior to start practising on examples of cut command, it is recommended that you know how to use cut command. So, visit cut command and go through all the points before working on the following examples.
Q1 Using the cut command, how do you extract out only the columns from 5 to 13 from data.csv in a file named foo?
$cut -d ',' -f 5-13 data.csv > foo
-d
option is used to specify the delimiter and -f
option is used to specify the columns to extract.
Q2 Suppose, you are having a file called 'records' with many lines of data, each line having values separated by a ':' character, how would you obtain the 5th value of each line using the cut command?
$cut -d ':' -f 5 records
Q3 Use the cut command to display file permissions
$ls -l | sed '1d' | cut -c 2-10
Output rwxr-xr-x rwxr-xr-x rwxr-xr-x r-xr-xr-x rwxr-xr-x rwx------
Q4 Use the cut command to display all of the group permissions
$ls -l | sed '1d' | cut -c 5-7
Output r-x r-x r-x r-x r-x ---
Q5 Consider the text file PEOPLE
101:Alice:Peters: President 102:Bob:Smith: VP 103:Jane:Jones: Programmer 104:John:Roberts: Project Manager
a) Use the cut command to display the first name and last name only.
$cut -d ':' -f 2,3 PEOPLE
b) Using cut and sort command and a pipe display the last name and job title in each record then sort the output by the last name.
$cut -d ':' -f 3,4 PEOPLE | sort -k1