Linux cut

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Linux cut

The 'cut' command is useful in selecting a specific column of a file. After (-d), delimiter (from where you want to separate the columns) comes. Delimiters can be a space (' '), a hyphen (-), a slash (/) or anything else. After (-f), column number is mentioned.

Syntax:

  1. cut -d(delimiter) -f(columnNumber) <fileName>  

Hyphen (-) As Delimiter

Syntax:

  1. cut -d- -f(columnNumber) <fileName>  

Example:

  1. cut -d- -f2 marks.txt  
  2. cut -d- -f1 marks.txt  

Linux Cut Filter1

Look at the above snapshot, our delimiter is hyphen (-), hence we have used (-) after (-d). Command "cut -d- -f1 marks.txt" displays column 1 and command "cut -d- -f2 marks.txt" displays column 2.

Space As Delimiter

If you want to use space as a delimiter then you have to quote the space (' ').

Syntax:

  1. cut -d ' ' -f(columnNumber) <fileName>  

Example:

  1. cut -d ' ' -f2 marks.txt  
  2. cut -d ' '-f5 marks.txt  
  3. cut -d ' '-f3 marks.txt 

Linux Cut Filter2

Look at the above snapshot, our delimiter is space, hence we have used (' ') after (-d). Command "cut -d ' ' -f2 marks.txt" displays column 2, command "cut -d ' '-f5 marks.txt" displays column 5 and command "cut -d ' '-f3 marks.txt" displays column 3.