cut command examples in Unix/Linux - Bash


cut command examples in Unix/Linux - Bash


                             Cut command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text.

In cut command -d stand for specifying delimiter -f will help to specify the column. You can specify delimiter as like follows,
-d" " --> space as delimiter
-d"." --> dot as delimiter
-d":" --> colon as delimiter
You can give any single character as delimiter, even alphabets and nummbers.

Please note the following variety of cut command samples. Here i am using dot as delimiter to give you some samples.

cut and print 2 to rest of all columns.
# echo www.domain.com | cut -d"." -f2-
domain.com

cut and print 2nd column.
# echo www.domain.com | cut -d"." -f2
domain

cut and print first to 2nd column.
# echo www.domain.com | cut -d"." -f-2
www.domain

cut and print 1st,3rd columns.
# echo www.domain.com | cut -d"." -f1,3
www.com

cut and print 1st and 4 to rest of all columns.
# echo a.b.c.d.e | cut -d"." -f4-,1
a.d.e

cut and print 1 to 3 and 5th columns.
# echo a.b.c.d.e | cut -d"." -f1-3,5
a.b.c.e

cut and print 3rd character.
# echo abcde | cut -c3
c

cut and print 3-rest of characters.
# echo abcde | cut -c3-
cde

cut and print first-3 of all characters.
# echo abcde | cut -c-3
abc

That's it...

This entry was posted by Arun Ghosh. Bookmark the permalink.

One thought on “cut command examples in Unix/Linux - Bash”