Untitled Document
cut Command

cut command is used to split or fetch the particular data from file . Command use either the column -c ( option) , -f (field) to be printed and ( -d )as a delimeter to cut the files in fetching the required fields . Lets see some examples of both the options on emp.csv which is a comma separated file .

1. Print Name , Designation and Salary from emp.csv -

We will use , ( comma) as a delimeter and -f will be used for printing columns . In emp.csv column 2 ,3 and 5 contains Name , Salary and Designation ,so we will provide thiese fields as an argument to cut command . Run the following command to see the result -

   cut -d "," -f 2,3,5 emp.csv 

2. Print the list of users ( usernames only ) who are logged in to current machine

We will have to use who command first and then we need to give o/p of to cut command . As the o/p of who is space / tab separated we will use space as a delimeter for cut command . The username are disapled in first column so for -f we will give 1 as field to be printed . Here is the command now -

   who | cut -d " " -f 1

3.With options -c

Print only the lines that are present in emp.csv and not emp1.csv, and vice versa.

 comm -3 emp.csv emp1.csv
Untitled Document