Linux cut command by example
The Cut command is a great utility to select parts of lines. In this post we’ll show many example of how to use it.
Select only 3rd character
echo "Hello World" | cut -b 3 l
Select only 1st and 7th characters
echo "Hello World" | cut -b 1,7 HW
Select characters in range
echo "Hello World" | cut -b 5-7 o W
Select beginning of the input line
echo "Hello World" | cut -b 1-5 Hello echo "Hello World" | cut -b -5 Hello
Select characters from 5th to the end
echo "Hello World" | cut -b 5- o World
Select characters except 3rd to 5th
echo "Hello World" | cut -b 3-5 --complement He World
Select columns from tab delimited input
echo "This is sample string." | cut -f 1-3 This is sample
Print only selected column/word in space delimited string
echo "This is sample string." | cut -d ' ' -f 2 is
Print first three words/columns in space deliminted string
echo "This is sample string." | cut -d ' ' -f -3 This is sample
Print words/columns from 3rd to the end
echo "This is sample string." | cut -d ' ' -f 3- sample string.