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.plIt will display the first character from each line of a file pg.pl
cut -c2-4 pf.jsIt will display the range of characters from 2nd to 4th from each line of a file pf.js
cut -c5- test.tclDisplays from 5th character to end of line for every line from a file test.tcl
cut -c-10 list.txtDisplays the first 10 characters from each line of a file list.txt
Split by delimiter
cut -d " " -f1 abc.cssSplits the line with space as delimiter and displays the first word from every line of the file abc.css
cut -d ":" -f3 xyz.plSplits 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.plDisplays 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" " -f2Content from sample file is split with space as delimiter and 2nd field is displayed.
cat sample | grep foo | xargs cut -d":" -f8Lines 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.