Search This Blog

Linux cut command with examples


cut is the most popular Linux and Unix command which will split the text into fields or columns. 

You can do any of the below operations using cut command

Split by characters
Split by delimiter 

Split by characters
cut -c1 pg.pl
It will display the first character from each line of a file pg.pl

cut -c2-4 pf.js
It will display the range of characters from 2nd to 4th from each line of a file pf.js

cut -c5- test.tcl
Displays from 5th character to end of line for every line from a file test.tcl

cut -c-10 list.txt
Displays the first 10 characters from each line of a file list.txt

Split by delimiter
cut -d " " -f1 abc.css
Splits the line with space as delimiter and displays the first word from every line of the file abc.css 

cut -d ":" -f3 xyz.pl
Splits the line with column ' : ' as delimiter and displays the 3rd column from each line of the file xyx.pl

cut -d ":" -f4,5,6 abc.pl
Displays multiple fields ( 4th, 5th and 6th ) after splitting wrt column ' : ' as delimiter

Cut command can also be used with other Linux commands
cat sample | cut -d" " -f2
Content from sample file is split with space as delimiter and 2nd field is displayed.

cat sample | grep foo | xargs cut -d":" -f8
Lines containing the word foo is extracted from sample file and it is split with column ' : ' as delimiter and the 8th field is displayed on the console.